1. what is the output of following program:
- package com.instanceofjava;
- public class SuperDemo{
- public void show(){
- System.out.println("Super class show() method called");
- }
- }
- package com.instanceofjava;
- public class SubDemo extends SuperDemo{
- public void show(){
- System.out.println("Sub class show() method called");
- }
- public static void main(String args[]){
- SuperDemo supobj= new SuperDemo();
- supobj.show();
- }
- }
2. what is the output of following program:
- package com.instanceofjava;
- public class SuperDemo{
- public void show(){
- System.out.println("Super class show() method called");
- }
- }
- package com.instanceofjava;
- public class SubDemo extends SuperDemo{
- public void show(){
- System.out.println("Sub class show() method called");
- }
- public static void main(String args[]){
- SuperDemo supobj= new SuperDemo();
- supobj.show();
- SubDemo subobj=new SubDemo();
- subobj.show();
- }
- }
3. what is the output of following program:
- package com.instanceofjava;
- public class SuperDemo{
- public void show(){
- System.out.println("Super class show() method called");
- }
- }
- package com.instanceofjava;
- public class SubDemo extends SuperDemo{
- public void show(){
- System.out.println("Sub class show() method called");
- }
- public static void main(String args[]){
- SuperDemo supobj= new SubDemo();
- supobj.show();
- }
- }
4. what is the output of following program:
- package com.instanceofjava;
- public class SuperDemo{
- public void show(){
- System.out.println("Super class show() method called");
- }
- public void superMethod(){
- System.out.println("Super class superMethod() called");
- }
- }
- package com.instanceofjava;
- public class SubDemo extends SuperDemo{
- public void show(){
- System.out.println("Sub class show() method called");
- }
- public void subMethod(){
- System.out.println("Sub class subMethod() called");
- }
- public static void main(String args[]){
- SuperDemo supobj= new SubDemo();
- supobj.superMethod();
- // supobj.subMethod(); // compile time error: The method subMethod() is undefined for the
- type Super
- ((SubDemo)supobj).subMethod();
- ((SuperDemo)supobj).show();
- }
- }
No comments