Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

plz explain the o/p

 
Mandar Jadhav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output of the above program is

value of name in parent object Base class
value of name in child object Base class

Can anyone please explain, how the child object 'getName()' method returns parent object 'name' variable value.
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both sub class and base class are refering to getName() method of the base class, getName() method has access to String variable name in base class only. getName() method can not see the members of its subclasses. So when we call this method by using base class reference variable or subclass reference variable it will always print the String name of the base class.

I hope I am able to clarify your doubt. Reply!
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was interesting fact!!!
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sandeep,
If I am wrong somewhere please rectify me. I will love to learn more.
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right dude...
 
Mandar Jadhav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the point....

Thanks gurneeraj .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar Jadhav,
Welcome to JavaRanch!

In an effort to help everyone get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
UseRealWords

Abbreviations such as "u" or "ur" in
place of "you" and "you are" or "you're" confound language translation software making
it hard for our non-English speaking members to read your posts.
"plz" is not a word in the English language.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was interesting fact!!!
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gurneeraj singh:
Both sub class and base class are refering to getName() method of the base class, getName() method has access to String variable name in base class only. getName() method can not see the members of its subclasses. So when we call this method by using base class reference variable or subclass reference variable it will always print the String name of the base class.

I hope I am able to clarify your doubt. Reply!


Please explain this



I get the output as


Value of name in parent object null
Value of name in child object null


Why is that ?
 
Sun Craven
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sridhar
please note the following :
public void Base()
is not a constructor,and the subclass is same to this.
The public void Base () method and the public void Sub were not benn invoked.Hence,the name variables are null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic