Any way to _not_ call superclass constructor in Java?

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

    The possibility is that you can call the super class constructor of your choice. That is possible by calling the super class constructor explicitly as :

    super(para_1, para_2,........);
    
    
    class BaseA {
        BaseA(){
            System.out.println("This is BaseA");
        }
        BaseA(int a){
            System.out.println("This is BaseA a");
        }
    
    
    }
    
    class A extends BaseA {
    
        A(){
            super(5);
            System.out.println("This is A");
        }
    
        public static void main(String[] args) {
            A obj=new A();
    
        }
    }
    
    
    
    Output will be:
    This is BaseA a
    This is A
    
    0 讨论(0)
  • 2020-12-15 04:19

    Every superclass needs to be constructed and there is no other way then calling a constructor.

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

    The closest you can achieve to the desired behaviour is to delegate initialisation normally performed in the constructor to a template method, which you then override in your subclass implementation. For example:

    public class A {
      protected Writer writer;
    
      public A() {
        init();
      }
    
      protected void init() {
        writer = new FileWriter(new File("foo.txt"));
      }
    }
    
    public class B extends A {
      protected void init() {
        writer = new PaperbackWriter();
      }
    }
    

    However, as other people have noted this can typically indicate a problem with your design and I typically prefer the composition approach in this scenario; for example in the above code you could define the constructor to accept a Writer implementation as a parameter.

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

    Every object in java is a subclass of Object (object with a capital 'O'). when you create an object of a subclass the super class constructor is invoked. Even if your class is not inhereting anyother class, implicitly it is inheriting Object, so the Object constructor has to be called. So super() is invoked for this purpose.

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

    Nope - you cannot do it and why would you want to do it anyway? That would mess up your object model.

    Anyways - i believe if you still want to do it and then you would have to manipulate the generated byte code.... there are a couple of libraries available that make it easy to instrument the byte code.

    Strongly suggest against doing it...

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

    Java deserialisation doesn't call the constructor, but it seems that it is based on some internal JVM tricks.

    However, there is a framework that allows you to do that in a portable manner: Objenesis (http://www.theserverside.com/discussions/thread/44297.html)

    I've seen this recently in Spring, when using CGLIB proxies, Spring creates two class instances, but the constructor is called only once: https://stackoverflow.com/a/11583641/2557118

    This behavior is added in Spring 4:

    CGLIB-based proxy classes no longer require a default constructor. Support is provided via the objenesis library which is repackaged inline and distributed as part of the Spring Framework. With this strategy, no constructor at all is being invoked for proxy instances anymore.

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