aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shadowing " Watch "shadowing " New topic
Author

shadowing

taras khoma
Greenhorn

Joined: Mar 14, 2011
Posts: 8


why the result is "parent"?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

You can't override fields. The title of this thread implies you're already familiar with the concept.
taras khoma
Greenhorn

Joined: Mar 14, 2011
Posts: 8
yes I'm familiar but we have object of type Child and not of type Parent.
And in java fields are accessible not by object but by reference(no polymorphism)

So take previous considerations we have object of type Child(NOT Parent) and invoke its method. So it must out Child field.
But in real we receive Parent field.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4364
    
    8

But you aren't accessing message via a Child reference. You're calling say() on a Child reference. That executes the code in Parent (because the method is not overridden in Child), and that's where you access message.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
Before subclass object getting instantiated the super class constructor gets called and its object gets instatiated. As say() method is not being overridden there super class say() mehod is being called and there it is using message field of its own. If you override the say() method in the subclass then it will be called.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

My arguments are simple Java facts :

fact 1: Child class have inherited the say() from parent. True
fact 2: To call the method of a class one must use object of that class. True
fact 3: Child object is created and inherited say() is called . True
fact 4: say() of Parent is called . Why?
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
Naveen,

say() of Parent class is called as say() is not overloaded in Child.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shadowing