File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes an inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "an inheritance question" Watch "an inheritance question" New topic
Author

an inheritance question

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183
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

Joined: Jan 10, 2002
Posts: 61415
    
  67

Please avoid posting in all uppercase. Please read this for more information.

I have adjusted the topic title for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
Good point ankit.. I was thinking about shadowing..was i wrong ?


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

have a look at this link..... may be useful.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Thanks all of you.
Ankit, your example helped clearing the concept.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an inheritance question