This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
M Rama
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sakthi Kani
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic