aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing in Inheritance 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 in Inheritance" Watch "Shadowing in Inheritance" New topic
Author

Shadowing in Inheritance

Sriram selvaraj
Greenhorn

Joined: Nov 28, 2008
Posts: 8
Hi Ranchers,
Can anyone explain me the reason behind the output of the following code,


Result: A:A

Why did we not get A:B

Thanks in advance.


Wish me good luck for my SCJP exam next week. :-)
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
I don't see any shadowing here, since you didn't provide any implementation of the getName() method in the B class then you will get the one from the parent class A. If you provide the implementation of that method in class B then you should get the output as A:B

Cheers!!!
Andriy Pererva
Ranch Hand

Joined: Jul 19, 2009
Posts: 73
Mo Jay wrote:If you provide the implementation of that method in class B then you should get the output as A:B

Or if you would use the field directly, instead of the getter method


SCJP 6.0(95%), SCWCD 5(94%), SCJD (working on B&S v.2.3.1)
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

remember that name is this.name


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Sriram selvaraj
Greenhorn

Joined: Nov 28, 2008
Posts: 8
Mo Jay wrote:I don't see any shadowing here, since you didn't provide any implementation of the getName() method in the B class then you will get the one from the parent class A. If you provide the implementation of that method in class B then you should get the output as A:B

Cheers!!!


Even if it uses A's implementation, the instance is of type B, so should it not use B's name ?

Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
If you want the method to do what you say then you should provide some method overriding for it where you can just copy the exact getName() method from the parent A to the subclass B and at that point B will be return it in the output. Remember that the copy you have of getName() method is a copy of the A class and class B did nothing to override or change that implementation, if you just expect the method to be inherited and magically to pick instance variable of the subclass then it is not going to happen that way.

You (the programmer) need to specify what the method should and what the output should look like by providing an implementation for it.
Sriram selvaraj
Greenhorn

Joined: Nov 28, 2008
Posts: 8
Mo Jay wrote:If you want the method to do what you say then you should provide some method overriding for it where you can just copy the exact getName() method from the parent A to the subclass B and at that point B will be return it in the output. Remember that the copy you have of getName() method is a copy of the A class and class B did nothing to override or change that implementation, if you just expect the method to be inherited and magically to pick instance variable of the subclass then it is not going to happen that way.

You (the programmer) need to specify what the method should and what the output should look like by providing an implementation for it.


Mo Jay, thank you for all your prompt replies. I understand that, since there is no implementation for getName in class B, it takes the parent's implementation. What I do not understand is

1. Instance as well as the variable type is of type B, so when getName is invoked, should it not take the value of name from B?
2. Again, this.name should be ideally pointing to B's reference because the instance and variable is of type B.



Thanks again everyone. And sorry if I sound a little dumb, I am really not able to get the underlying point behind this.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sriram selvaraj wrote:
2. Again, this.name should be ideally pointing to B's reference because the instance and variable is of type B.




why confused? your are calling getName() (which is in A class also implicitly in B since B is a subclass of A) twice . which has the return value of A's name .
Tanya Shetty
Ranch Hand

Joined: Jun 17, 2009
Posts: 40
Hi,

In overriding, only methods are overriden, constructors, static methods(which appear to be overriden but are actually only re-defined) and variables are NOT overridden by the subclass.

In your example, when b.getname() is invoked, you can picture something like this is happening...


So, when b.getname() invokes the getname() method from A, it fetches the "name" field of A, because instance variables are NOT overridden and while B object is in the superclass, B will fetch the instance variable which belongs to that class from where the instance method is being called...

If however, this getname() method was in B, it would have picked the "name" field from B.. if the corresponding name field is absent in B, b.getname() will pick up the "name" field from A, because it inherits this "name" field.

It might be interesting to note that, if you changed your code so that,



This proves, that even though, there is a "name" field in B, which appears to override "name" in A, it does not! or else output would have been B:B..

I hope this helps to clear your doubts..

Sriram selvaraj
Greenhorn

Joined: Nov 28, 2008
Posts: 8
I understood now. Thanks Tanya for the detailed explanation. Thanks for all the ranchers for the prompt replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shadowing in Inheritance