This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on parent-child members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on parent-child members" Watch "Question on parent-child members" New topic
Author

Question on parent-child members

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Question

parent class has a member String test = "parent";

child class has

String test = "child";

i instantiate a child class instance with parent class reference in some third class

what will the child class instance have in its test member varible? when i access the member in parent class member method (USing this reference) and when i access the member in child class method method ((USing this reference))
???


SCJP, SCWCD, SCBCD
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Understood !

Answer: The instance will have both the members so it will have two members test="parent" and test="child" and depending on the class where its accessed the value will be so. If accessed from parent class member method as this.temp, here temp would point to "parent" member and if accessed from child member method it will point to child class member method and hence have the value "child".



 
Don't get me started about those stupid light bulbs.
 
subject: Question on parent-child members