2

I am asking this here because I really don't know how I should Google this. I need a confirmation of something that I probably figured out today. Some days ago I thought that if an object inherits a method of its father it means that it basically "has" the method in its code. But then I asked what if we do this:

class B extends A {

  public B() {
  }

  public int getValue() {
     return 2 * super.getValue();
  }

}

class C extends B {

  public C() {
  }

  public int getValue(int b) {
     return 5 * b;
  }
}

And lets say there is a class A that has the getValue() method too. Now an object of class C uses "its" getValue() method (without parameter) that it has inherited from B.

But super.getValue() still refers to class A, even though the method calling it was inherited.

Inheritance does not mean that class C "has" the method in its code, but that it can use it from B, right? super() still refers to the superclass of its "original" class.

Is that insight correct?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
yemerra
  • 1,352
  • 4
  • 19
  • 44

2 Answers2

2

Inheritance does not mean that class C "has" the method in its code, but that it can use it from B, right?

Yes.

super() still refers to the superclass of its "original" class.

Yes. You are correct. super always refers to the Parent class of where it is being used. In this case it is being used in B and it's Parent is A. So referring to there.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    I disagree with your "No" answer. C inherits all the non-private methods of A and B, regardless of whether it overrides any of them or not, so in that sense it "has" them. – Eran Aug 26 '14 at 12:57
  • @Eran `but that it can use it from B`. My bad. Read twice and making sense. Thanks for that correction. – Suresh Atta Aug 26 '14 at 12:58
  • But if I have a C object and C inherits the method and uses this method, then it means the method refers to itself in B, but that iis not the case here. Even though C inherits the method, super still refers to the A method. Right? – yemerra Aug 26 '14 at 13:06
  • 1
    @DennisvonEich Child can access the method from Parent (Inherit), but that doesn't mean that it is having the method. Still the parent only contain that method. You are just executing it from Child's location. Hope that clear. – Suresh Atta Aug 26 '14 at 13:09
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ Thats exactly what I wanted to know. That was an important fact that I didn't know before. Thank you! – yemerra Aug 26 '14 at 13:13
0

A sub-class can override any non-private method of its super-class. If it doesn't override them, it inherits the implementation of these methods from its super-class.

Therefore you can say that the sub-class "has" access to all the non-private methods of all of its ancestors. And from a reference to an instance of a sub-class you can invoke any accessible (based on the access modifiers) method of the sub-class or any ancestor of the sub-class.

The super keyword allows you to invoke a method implementation of the super-class from the sub-class. This way you can override the implementation of a method of the super-class, but still execute the implementation of the super-class too.

In your class B :

This implementation overrides the getValue() of A :

  public int getValue() {
     return 5;
  }

While this implementation overrides the getValue() of A, but still uses its logic :

  public int getValue() {
     return 2 * super.getValue();
  }

Of course, you can't implement both in the same class, since they have the same signature.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    The main confusion in the question seems to be around the use of the word `super` in this context. I think this answer misses that. – Duncan Jones Aug 26 '14 at 12:51