• 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

an inheritance question

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Niko's Blog.




The output is Parent Parent

I cannot understand why line #2 prints "Parent". Child class does not override the say()
method but it does inherit it. Its like :-

class Child extends Parent {

String message = "child";

void say() { //INHERITED MOETHOD
System.out.println(message);
}

}

So shouldn't it print the value of the message variable of the Child class as reference type is Child for yo1. I think I am missing out on something very basic about inheritance .....
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid posting in all uppercase. Please read this for more information.

I have adjusted the topic title for you.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simran, the Child class doesn't inherit the say method as you are saying. Lets see an example
Now if the Child class inherits say as you are saying, then what will happen at runtime?? The statement String m = message; will try to assign an int to a String. So basically since the say method is in Parent class, access to instance fields from that method will always point to instance fields of Parent class. The rule is, access to methods is polymorphic but access to fields is not polymorphic...
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point ankit.. I was thinking about shadowing..was i wrong ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have a look at this link..... may be useful.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit Garg said,

but access to fields is not polymorphic...



Then what is that? Can give the exact answer.

See this coding...

.
Here the variable 'message' inherited downward to Child class???,,,,
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:Ankit Garg said,

but access to fields is not polymorphic...



Then what is that? Can give the exact answer.

Here the variable 'message' inherited downward to Child class???,,,,



Polymorphism generally means that there is a mechanism that determines the latest version of a method (or variable, if supported) at runtime. In the case of Java, methods go through a jump table to make sure that the latest version is called.

For static methods (or field variables) which doesn't support polymorphism, this mechanism doesn't exist. Instead, the target is determined at compile time. In the case of your latest example... yes, the compiler is smart enough to realize that the field doesn't exist in the child (when compiling the child class), and hence, uses the parent instead. So, it does "look" sortof like polymorphism...

But it isn't. Fields are not inherited. And the compiler will not get this right all the time. As shown in your first example.

Henry

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks all of you.
Ankit, your example helped clearing the concept.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic