• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I've aqustion on inheritance and shadoing of parent class primitive variables.

In this code when i assign a to c i.e a=c ;
Both a.eat() and c.eat() are returning the same output i.e "Horse's eat"..
But the a.weight() results in Animal's weight(100) and c.weight results in Horse's weight.(30)..

My question is if you assign a reference to another ,will the methods and primitive variables be the same for both the references (in this case 'a' and 'c'.
(Why not the primitive variables a.weight and c.weight are not the same.
I know that primtivie variables wll not be overridden,but the subclass variable can shadow the superclass's varibale.
(Is this b'coz the primitives will be stroed in stack???")



Before assigning a to c.i.e a=c;
The a.eat() mthod results in 'Animals'eat' and c.eat() returns 'Horse's .eat'.Before assignment a.weight results in 100 and c.weight results 30.(which is same after assignmnet also)..
Can anyone please explain?? Thanx in advance
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, there isn't a weight() method, so I am assuming you mean the instance variable.

It is all about the question of the JVM using reference variable type verseus actual object type.

When you get an instance variable using

a.weight it will return the instance variable of the class of the reference type, not the actual instance type.

if you call a method it will run the methond in the actual instance type

Aniaml a = new Horse()

a.weight is the wieght in Animal and a.eat() is the eat method in Horsse.

Hope that helps

Mark
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After the assignment a = c, both of these variables reference the same object, which is an instance of Horse. However, by assigning the Horse reference to a variable of type Animal, the reference is automatically upcast to type Animal. This is called "assignment conversion."

Now, because the variable "a" is of type Animal, a.weight accesses the weight variable in Animal. And because the variable "c" is of type Horse, c.weight accesses the weight variable in Horse.

But unlike variables, methods can be overridden. And due to polymorphism, a.eat() invokes the overridden method body of eat().

See the JavaRanch article, How my Dog learned Polymorphism.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fundamental rule of Java's polymorphism:

Overriden methods are resolved at runtime based on the object's instance type.
Overloaded methods are resolved at compile time based on the reference type.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here, We Have To Keep In Mind Is That The Method Lookup Is Always From Bottom To Up. That Is The Methods In Child Class Always Gets Called (As They Are Overridden And Child Is A Parent) But The Variable Look Up Is From Up To Down. That Is Variables Of Parents Gets Displayed If The Refrence Variables Are Of Parent Type.
 
reply
    Bookmark Topic Watch Topic
  • New Topic