0

Test.java

public class Test {

    public void go(){       
        System.out.println("Test go");
    }
}

Test2.java

public class Test2 extends Test {
public void go(){       
    System.out.println("Test 2 go");
}

public void back(){
    System.out.println("Test 2 back");
    }   
}

class Demo{

    public static void main(String[] args) {

    Test t=new Test2();
    t.go(); // Output : "Test 2 go"
    t.back(); //Compile time error. 
 }

 }

I read some issues on stackoverflow regarding this but I did not understood the meaning of SuperClass s=new SubClass();. Also in output if Object of Test can access the go() method of Test2 then why it cannot access back() method.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Ashraf Mulla
  • 133
  • 2
  • 14
  • See also the official language tutorial: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html – Jason C Sep 28 '16 at 13:11

1 Answers1

0

This is an example of Polymorphism, which means we can refer to subtype object using reference of super type.

back() method is not defined for Test type . You are calling back() method on Test type variable which is not valid. When you declare

Test t = new Test2();
  • You are creating a reference variable t of type Test
  • You are referring to an object which is of type Test2 using polymorphic reference.

Also in output if Object of Test can access the go() method of Test2 then why it cannot access back() method.

As t is of type Test, it can only know about methods defined in Test class. It cannot know about methods which are defined in subclass Test2

Regarding your question in comment,

  • When you say t.go(), at compiler is thinking go() method from Test class is being called. At compile time it is not known which object is going to be created.
  • Your declaration Test t = new Test2(); creates Test2 object at runtime, which in turn calls go() method from Test2 because it is overriding go() from Test

You should really read about compile time polymorphism and runtime polymorphism

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • if "It cannot know about methods which are defined in subclass Test2" then the output of t.go() has to be "Test go" means it should call go() method from Test. Why it is calling go() method of Test2? – Ashraf Mulla Sep 28 '16 at 11:50
  • @AshrafMulla prasad has given it's valid reasons to explain ,you can give a try to my answer for reading if you want – Pavneet_Singh Sep 28 '16 at 11:54
  • 1
    Yes @PavneetSingh this question came in mind due to your explanation "It's polymorphic statement where parent reference variable can hold the reference of child object." and Prasad Khamkar's " It cannot know about methods which are defined in subclass Test2". Now I understood the process of polymorphism. Thank you very much both of you. – Ashraf Mulla Sep 28 '16 at 13:01
  • 1
    @PrasadKharkar yes that was a very good explanation for my question. Thank you for it and additional reference links. – Ashraf Mulla Sep 28 '16 at 13:02
  • One think, when I write Test t = new Test2(); Is it instance variable of Test i.e.'t' behaves as if it is an instance of class Test2 OR is it the instance of class Test2 that behaves as if it is an instance of Test? – Ashraf Mulla Sep 29 '16 at 07:10
  • I took the reference of this sentence:"Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. For example, in Java all classes inherit from Object. Therefore, you can create a variable of type Object and assign to it an instance of any class." – Ashraf Mulla Sep 29 '16 at 07:12
  • There is only one instance, Test2() which is being referred by reference variable t of type Test. Nothing more. Test2 doesn't behave like it is instance of Test. Test t = new Test2() simply means that we are creating an instance of Test2 and it is being referred by reference variable t of type Test. This is possible because Test2 extends Test and t is polymorphic reference to instance of Test2 – Prasad Kharkar Sep 29 '16 at 08:11