1

I have a doubt about if what I want to do is possible. I have three classes A, B y C. B extends A and C extends B.

C -> B -> A.

In C class, I want to call a method directly of A class. Something like

super.super.method();

do I have any chance of do it?

The Reason:

A and B are system classes. I am trying to make B handle some situations in a different way and I have accomplished it except for one method. In other methods what I did is to override them and pass the arguments modified.

Now I have a method that I cannot solve just modifying the arguments so I want to fully override it.

B

@Override
public void draw(Canvas canvas) {
    super.draw(canvas); // A method
    ** other stuff **
}

What I want to do in C

@Override
public void draw(Canvas canvas) {
    super.super.draw(canvas); // A method
    ** new stuff **
}
Juangcg
  • 1,038
  • 9
  • 14
  • 2
    http://stackoverflow.com/questions/11935895/java-how-to-call-super-super-in-overriden-method-grandparent-method Perhaps "google" your query before posting here? – Ben Dale Aug 29 '13 at 08:54
  • I didn't find them before posting :(. Thanks for the links. Should I delete the answer? I'm kind of newbie. – Juangcg Aug 29 '13 at 09:06

5 Answers5

4

Picked up the most appropriate answer from here by Jon Skeet

You can't - because it would break encapsulation.

You're able to call your superclass's method because it's assumed that you know what breaks encapsulation in your own class, and avoid that... but you don't know what rules your superclass is enforcing - so you can't just bypass an implementation there.

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

You cannot. super only gives you access to the direct superclass.

If you think need to do that you probably have a design issue. Can you please give us more details about what you would like to do?

mariosangiorgio
  • 5,520
  • 4
  • 32
  • 46
0

I don't think that is possible. May be you can implement this way.

class B

method(){
super.method()
}

Then in class C you can call super.method(). That way you'll eventually call A's method.

Crickcoder
  • 2,135
  • 4
  • 22
  • 36
0
class A
{
  void access()
  {
   //this method is to be accessed
    System.out.println("I can be accessed");
  }
}
class B extends A
{
  //code here
}

class C extends B
{

  public static void main(String[] args)
  {
    C ob = new C();
    ob.call();
  }

 void call()
  {
  super.access();
  }
}
ofnowhere
  • 1,114
  • 2
  • 16
  • 40
-2

super can be used to call your direct parent class .

May be you can design a class B function such that the super would call class A . Now you can use class C to achieve the desired result .

Shuhail Kadavath
  • 448
  • 3
  • 13