wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes the complier looks only at the reference type not the instance type for overridden methods!!???!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the complier looks only at the reference type not the instance type for overridden methods!!???!!" Watch "the complier looks only at the reference type not the instance type for overridden methods!!???!!" New topic
Author

the complier looks only at the reference type not the instance type for overridden methods!!???!!

Sbt Bhatta
Greenhorn

Joined: May 21, 2009
Posts: 9
Hello, I have a question after reading the SCJP book by kathy and bert

It says " the complier looks only at the reference type not the instance type [for overridden methods]"

Now please look at the following codes::




Why does at Q1 it suns the animal version of eat whereas at Q2 it runs the horse version. In the Kathy book, it says the compiler looks at the compiler version.


In both Q1 and Q2, isn't the reference type is Animal ?
SO I am guessing somewhere I understood it wrong. It seems to me as if the JVM looks at the instance type during runtime/compile time. please please pretty please
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi Bhatta,

It is very well written in the book. "the COMPILERlooks only at the reference type not the instance type [for overridden methods]". But at runtime, it checks the instance type. For example if you try to call



I guess it will not let you compile the program. Try this.

Regards,
Patricia.
Sbt Bhatta
Greenhorn

Joined: May 21, 2009
Posts: 9
Patricia Samuel wrote:Hi Bhatta,

It is very well written in the book. "the COMPILERlooks only at the reference type not the instance type [for overridden methods]". But at runtime, it checks the instance type. For example if you try to call



I guess it will not let you compile the program. Try this.

Regards,
Patricia.


So at compile time, the JVM will look at the instance type to make sure the methods actually exists . Then at runtime, the JVM will chose the correct overridden method. Am I right? Please say yes.
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Sbt Bhatta wrote:In both Q1 and Q2, isn't the reference type is Animal ?
SO I am guessing somewhere I understood it wrong. It seems to me as if the JVM looks at the instance type during runtime/compile time. please please pretty please


It's probably just a question of terminology.

Animal a = new Animal();
Animal b = new Horse(); //Animal ref, but a Horse object
a.eat(); // Q1
b.eat(); // Q2

In this example it's the type of the actual object that's important, not the formal type of the variables. So which overridden method that's being called is determined by the actual object type, not the formal variable type. And this selection is made at runtime. (It must be that way because objects don't exist at compile time).

This more clearly demonstrates that it's the actual object type that's involved in the method selection,

new Animal().eat();
new Horse().eat();

A formal variable isn't even involved, still the correct eat method is selected.

Note that when methods are overloaded it's the other way around. Which method to call is determined by the formal variable type and this is decided at compile time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the complier looks only at the reference type not the instance type for overridden methods!!???!!
 
Similar Threads
Refrence type or Instance type
Dout about overloaded methods
Polymorphism
overloaded method call rules
which version of method is actually called ? the reference one or actual object one ?