24

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

I have 3 classes they inherit from each other as follows:

A
 ↳
   B
    ↳
     C

Inside each class I have the following method:

protected void foo() {
    ...
}

Inside class C I want to call foo from class A without calling foo in B:

protected void foo() {
    // This doesn't work, I get the following compile time error:
    // Constructor call must be the first statement in a constructor
    super().super().foo(); 
}

EDIT
Some Context Info:
Class B is an actual class we use. Class C is a unit test class, it has some modifications. foo method inside B does some things we don't want so we override it inside C. However foo in class A is useful and needs to be called.

Community
  • 1
  • 1
Caner
  • 57,267
  • 35
  • 174
  • 180
  • 1
    Take a look at this topic http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java – Pr0gr4mm3r Aug 13 '12 at 14:07

2 Answers2

23
  • To call a method in a super class, you use super.foo(), not super().foo(). super() calls the constructor of the parent class.
  • There is no way to call super.super.foo(). You can add a call to super.foo() in class B, so that calling super.foo() in C, will call super.foo() in B which in turn will call foo() in A.
assylias
  • 321,522
  • 82
  • 660
  • 783
15

It's not possible in Java. You'd need to rely on B providing you with an explicit way to access to A's foo.

Romain
  • 12,679
  • 3
  • 41
  • 54