Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sbt Bhatta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic