Azeez
Azeez

Reputation: 328

How to call a method in super class of super class of a subclass

The hierarchy of Inheritance is like this.

A --> B --> C

and Sample code is like below

Class A{
  public void method(){....}
}
Class B extends A{
  public void method(){.....}
}
Class C extends B{
  public void method(){......}
}

To access the method() of A from Class C can I write the code like This, in the method() of C;

public method(){ //To call method() of A, when we call method() in C.
   Object obj=super.super();
   obj.method();
}

Whether this code will work for my scenario or not?. If it is wrong, please suggest me your answers.

Upvotes: 1

Views: 179

Answers (5)

Qadir Hussain
Qadir Hussain

Reputation: 1263

I would suggest you to do something like this.

Class A{
  public void method(){
    // implement your code here.
  }
}
Class B extends A{
  public void method(){
    super.method(); // will call method of A
  }
}
Class C extends B{
  public void method(){
    super.method();  // will call method of B
  }
}

Upvotes: 0

iamyogish
iamyogish

Reputation: 2432

No this can't be done in java. You will get a compiler error like : There is error in line “super.super.method();”. In Java, a class cannot directly access the grandparent’s members. Though it is allowed in C++ though. In C++, we can use scope resolution operator (::) to access any ancestor’s member in inheritance hierarchy. In Java, we can access grandparent’s members only through the parent class. How ever u can do this in the method() of class B and Class C :

void method(){
// method of class B
super.method(); // calls method() of class A
}

  void method(){
 // method of class C
super.method(); // calls method() of class B
}

Hope this helps..

Upvotes: 0

Adrian Shum
Adrian Shum

Reputation: 40036

I am not sure if it is the intention of the designer of Java language, but I would understand by this: With only single inheritance, when C inherits from B, C should only care about its immediate parent which is B. If B decided to hide anything of A from its child, C shouldn't be able to see that.

There are lots of way to workaround it, for example:

Assuming in design of A, you foresee such situation and want any level of child class to be able to make use of your method, you can create a protected one for child to use:

class A {
    protected void methodInternal() {...}
    public void method() { methodInternal(); }
}

class B extends A {
    public void method() {
        someOtherLogic();
    }
}

class C extends B {
    public void method() {
         methodInternal();
         somethingElse();
    }
}

If it is developer of B that decide to change method's impl but it still want to leave flexibility of letting child use A's method, he can do something like:

class A {
    public void method() {  }
}

class B extends A {
    public void method() {
        someOtherLogic();
    }
    protected void originalMethod() {
        super.method();
    }
}

class C extends B {
    public void method() {
         originalMethod();
         somethingElse();
    }
}

Upvotes: 0

ajb
ajb

Reputation: 31689

You cannot do this in Java. First of all, super.super() doesn't make sense. (super(); is useful inside a constructor, but outside of a constructor it doesn't mean anything.) Second, if you have an object of type C, you cannot create an object variable that is "the same object, but of type A", which is what you're trying to do. The runtime type of the object will always be C. So even though this is legal:

public void method() {
    A obj = this;
    obj.method();
}

even though obj is declared as type A, its "real" type at runtime will always be C, and the above code will call the method() in C, which is infinite recursion.

You can have method() in C call method() in B like this:

public void method() {
    super.method();     // calls the method in B
}

But there's no way to skip over this and call method in A:

public void method() {
    super.super.method();     // illegal
}

And if you think you want to do this, then you probably need to rethink your design. An overriding method generally either calls the parent's method and adds something new to it, or it does something completely new; it usually doesn't make a lot of sense for it to add something new to the grandparent method but skip over something new that was added in the parent method. If, after all that, you really still want to do that, you could accomplish it by adding a new protected method in A with a different name.

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

No, method() in A will override by method() in B and then method() in B will override by method() in C then you can't call method() in A from C.

Upvotes: 0

Related Questions