4

Possible Duplicate:
Why is super.super.method(); not allowed in Java?

If you have a class that derives from a class that derives from another is there anyway for me to call the super.super method and not the overridden one?

class A{
    public void some(){}
} 

class B extends A{
    public void some(){}
} 

class C extends B{
    public void some(){I want to call A.some();}
} 
Community
  • 1
  • 1
arinte
  • 3,660
  • 10
  • 45
  • 65

2 Answers2

5

See: Why is super.super.method(); not allowed in Java?

Community
  • 1
  • 1
Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96
3

@tgamblin is right but here is a workaround :

class A{
    public void some(){ sharedCode() }
    public final void someFromSuper(){ sharedCode() }

    private void sharedCode() { //code in A.some() }
} 

class B extends A{
    @Override
    public void some(){}
} 

class C extends B{
    @Override
    public void some(){
     //I want to call A.some();
     someFromSuper();
    }
} 

Create a second version of your method in A that is final (not overridable) and call it from C.

This is actually a poor design, but sometimes needed and used inside JDK itself.

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173