I have a class that extends a class that I need to overide, but I need to call that class’s parent class. since I can’t call super since that will execute the direct parent what is the best way to get the parent of my parent class?
I am sure this is a basic question, but its been a while since I have been doing any java.
class A
{
public void myMethod()
{ /* ... */ }
}
class B extends A
{
public void myMethod()
{ /* Another code */ }
}
class C extends B
{
I need to call Class A here
{ /* Another code */ }
}
You don’t: it violates encapsulation.
It’s fine to say, “No, I don’t want my own behaviour – I want my parent’s behaviour” because it’s assumed that you’ll only do so when it maintains your own state correctly.
However, you can’t bypass your parent’s behaviour – that would stop it from enforcing its own consistency. If the parent class wants to allow you to call the grandparent method directly, it can expose that via a separate method… but that’s up to the parent class.