• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

an inheritance question  RSS feed

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Good point ankit.. I was thinking about shadowing..was i wrong ?
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at this link..... may be useful.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


Thanks all of you.
Ankit, your example helped clearing the concept.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!