Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Inheritance  RSS feed

 
Ranch Hand
Posts: 91
  • 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?
 
Ranch Hand
Posts: 143
  • 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.
 
Ranch Hand
Posts: 98
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!