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

Shadowing in Inheritance

 
Sriram selvaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember that name is this.name
 
Sriram selvaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood now. Thanks Tanya for the detailed explanation. Thanks for all the ranchers for the prompt replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic