Say you have a class B that extends a parent class A. To access the methods of B, you would use this. To access A, you would use super. If you were to have a third class C that extends class B, you would use this to access methods of C and super to access B. How would you access the methods of A from class C?
-
4this may help http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java – ford prefect Nov 22 '13 at 18:58
-
You Don't need to call super if you're not overriding anything. You need to give a better explanation. – Paul Samsotha Nov 22 '13 at 19:22
-
"you would use this to access methods of C and super to access B" This is fundamentally wrong. `super` can be used to access the method implementation in the superclass of the class that the code is in. So accessing a method with `super` in class B, would access the method in class A (or superclass thereof), even if the current object has class C (or subclass thereof). `super` is a completely different way of method access. – newacct Nov 25 '13 at 08:50
2 Answers
You actually can't because it will violate principle of encapsulation. However you can create a method in class B that does same as that of A and get the desired result.

- 4,890
- 3
- 35
- 46
Let's use this example:
class Bird {
public final void eat() {
//...
}
public boolean canFly() {
return true;
}
}
class FlightlessBird extends Bird {
public boolean canFly() {
return false;
}
}
class Ostrich extends FlightlessBird {
public void takeoff() {
if (canFly()) {
//....
}
}
}
Ostrich extends FlightlessBird so canFly()
will always return false. If you could, however call super.super.canFly()
you'd end up with a flying ostrich and that's nothing anyone wants to see. Basically, methods are overridden for a reason and going around that can cause disastrous results. If you need a method in all child classes then mark it as final so it can't be overridden (like eat in the example above). If you're using someone else's library and you can't change the grandparent then you need to inherit directly from the grandparent.
Also, if you find yourself needing to use super a lot then you're probably doing something wrong.

- 437
- 2
- 12