Any way to _not_ call superclass constructor in Java?

后端 未结 13 1317
误落风尘
误落风尘 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:29

    No and if you could, your derived object wouldn't really be the object it's deriving from now would it? The is-a principle would be violated. So if you really need it, then polymorphism isn't what you're after.

    0 讨论(0)
  • 2020-12-15 04:33

    I had a similar requirement where I needed my child class NOT to go through the super class' constructor, and I wanted the rest of the benefits of the super class. Since super class is also mine, here's what I did.

    class SuperClass {
        protected SuperClass() {
            init();
        }
    
        // Added for classes (like ChildClassNew) who do not want the init to be invoked.
        protected SuperClass(boolean doInit) {
            if (doInit)
                init();
        }
    
        //
    }
    
    class ChildClass1 extends SuperClass {
        ChildClass1() {
            // This calls default constructor of super class even without calling super() explicitly.
            // ...
        }
        // ....
    }
    
    class ChildClass2 extends SuperClass {
        ChildClass2() {
            // This calls default constructor of super class even without calling super() explicitly.
            // ...
        }
        // ....
    }
    
    class ChildClassNew extends SuperClass {
        ChildClassNew() {
            /*
             * This is where I didn't want the super class' constructor to 
             * be invoked, because I didn't want the SuperClass' init() to be invoked.
             * So I added overloaded the SuperClass' constructor where it diesn;t call init().
             * And call the overloaded SuperClass' constructor from this new ChildClassNew.
             */
            super(false);
            // 
            // ...
        }
        // ....
    }
    
    0 讨论(0)
  • 2020-12-15 04:37

    If you don't want to call the superclass constructor, there is something else wrong with your object model.

    0 讨论(0)
  • 2020-12-15 04:38

    There is absolutely no way to do this in Java; it would break the language specification.

    JLS 12 Execution / 12.5 Creation of New Class Instances

    Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

    1. Assign the arguments for the constructor [...]
    2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then [...]
    3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super).
    4. Execute the instance initializers and instance variable initializers for this class [...]
    5. Execute the rest of the body of this constructor [...]
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 04:39

    I think the only way to do it is messing up with the byte-code.
    I'm not sure if the Classloader or the JVM checks if super() is being called, but, as Bozho wrote, you probably would end with inconsistent objects when doing so.

    0 讨论(0)
提交回复
热议问题