GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91


Prints:

Parent Class
Parent
Parent Class
Parent

whereas:



Prints:
Child Class
Child
Child Class
Child


I understand the static method printing. But not the instance variable str.

Is this material covered in K&B( Chapter 5 doesn't have it and I am yet to read 3 and 6 from first 9 )? Is there a different book I need to read for this?
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
Static methods are cannot be overriden. So it is execute current class methods like print(). Once again I noticed, Instance members are cannot be overriden. It is hiding. In Overriding Methods are executed based on the Object reference. But memebers variables ae accessed based on class. ie., classname.variable. If u have any more doubts walk through the code available in your previous posting
http://www.coderanch.com/t/248659/java-programmer-SCJP/certification/abstract-class-protected-member-behaviour.


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hai rama,
as far as ur program is concerned,u r trying to access a static method.
in general,static methods take the reference and not objects & so u get the result (as mentioned in ur 1st code).


----------------------------------------------------------------------------

I understand the static method printing. But not the instance variable str.

----------------------------------------------------------

instance variables are similar in behaviour to static methods,they too refer only the reference and not the objects.
hence according to ur code(both 1st & 2nd ),u r creating the reference for the class & trying to print the str(instance variable)from that class,since instance variables refer to references & not objects,it takes the value os str in that particular class which it refers to.

if still not clear,plz reply

regards
-sakthi


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
 
GeeCON Prague 2014
 
subject: Inheritance