Any way to _not_ call superclass constructor in Java?

后端 未结 13 1366
误落风尘
误落风尘 2020-12-15 04:16

If I have a class:

class A {
   public A() { }
}

and another

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

is t

13条回答
  •  一生所求
    2020-12-15 04:38

    Assuming you mean

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

    then sure you can

    class B extends A {
         public B() {
             this(abort());
         }
         private B(Void dummy) {
             /* super(); */
         }
         private static Void abort() {
             throw null;
         }
    }
    

    Not very useful. The interface [not Java keyword] to class A says that you need to run its constructor in order to construct it, not unreasonably. The exception is that serialisable classes are constructed without calling the constructors of the serialisable classes.

提交回复
热议问题