• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Polymorphism: reference variable type and subclass method call

 
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's for your further consideration:
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic