File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an inheritance question

 
Simran Dass
Ranch Hand
Posts: 183
  • 0
  • 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 .....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64204
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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...
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point ankit.. I was thinking about shadowing..was i wrong ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • 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
  • 0
  • 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???,,,,
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks all of you.
Ankit, your example helped clearing the concept.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic