• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism: reference variable type and subclass method call  RSS feed

 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello my friends,

I am still at learning java. I have a question regarding an exercise i am doing here. At the same time I am reading "Head First Java" and on page229, there is written:

"You can call methods on an object only if the methods are in the class (or interface) used as the reference variable type, regardless of the actual object type. So, a reference variable of type Object can be used only to call methods defined in class Object, regardless of the type of the object to which the reference refers."

So regarding the following code:







Why is the output of class TestAnimal "Meow!"?
The case is reference variable type is "Animal", but Animal.greeting() is abstract.

Can somebody please help me understand this please?

Best,
A.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making a Cat object and assigning it to an Animal variable. Notice both have a greetings() method. So Animal has a interface point for a method called greetings(), Cat supplies that interface point, it is an Animal, with its greetings() method.

consider this:

U. Remmarot wrote:Hello my friends,

I am still at learning java. I have a question regarding an exercise i am doing here. At the same time I am reading "Head First Java" and on page229, there is written:

"You can call methods on an object only if the methods are in the class (or interface) used as the reference variable type, regardless of the actual object type. So, a reference variable of type Object can be used only to call methods defined in class Object, regardless of the type of the object to which the reference refers."

So regarding the following code:







Why is the output of class TestAnimal "Meow!"?
The case is reference variable type is "Animal", but Animal.greeting() is abstract.

Can somebody please help me understand this please?

Best,
A.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's for your further consideration:
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing to maybe help.

When dealing with cases like this, there are two questions that you need to answer:

#1 - is it valid to call this method on my variable?
#2 - if I call it, which version of the method is called?

The first of these can be answered at compile time. It is based on the declared type of your variable.
Your variable animal1 is declared as being of type Animal.
Animal has the "greeting" method declared , so making this call is allowed.

The second question - which version is run can only be answered at runtime.
Variable animal1 is declared as being of type Animal, but it ACTUALLY has a Cat object.
So we the method that gets called is Cat's version of greeting.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!