This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!