Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip one level in inheritance calling super from grandparent in java?

I have simple three classes:

class A {
  public A(int a){ }
}

class B extends A {
  public B(int b){ super(b); }
}

class C extends B {
  public C(int c){ super(c); }
}

So, the order of execution during class instancing is C->B->A->B->C and all objects are instantiated correctly. Then, the question:

CAN I in some way write a constructor for C class like this:

  public C(int c){
    super.super(c);
  }

The idea is to call the constructor from A class, not from immediate parent B. Is this possible?

like image 570
piotao Avatar asked Nov 04 '16 10:11

piotao


1 Answers

No you can't do that.

All classes in a heirarchy need to be constructed. You cannot bypass the construction of B.

What you could do is write a protected constructor in B that's essentially a no-op, and call that from the constructor in C.

like image 168
Bathsheba Avatar answered Nov 15 '22 15:11

Bathsheba