Is a subclass needed to create a constructor?

I learned about inheritance, and I was just curious. I know that a subclass will automatically call the constructor of the superclass, even if you are not using the super() operator, so I wanted to know if the subclass even needed a constructor in it.

+6
source share
4 answers

A subclass needs a constructor if the superclass does not have a default constructor (or has one that is not available for the subclass). If the subclass does not have a constructor at all, the compiler will automatically create a public constructor, which simply calls the default constructor of the superclass.

Regarding calling super() : the first thing every constructor should do is either call another constructor in the same class by calling this() (possibly with some arguments), or call the constructor of its superclass by calling super() (again, possibly with arguments). None of these calls can go anywhere. If the constructor does not start with any, the compiler automatically inserts the call into super() (without any arguments). Therefore, if you want, you must refer to the default superclass constructor (and, many times, this is the case), then you do not need to explicitly call super() yourself.

There is also one situation where you do not need to provide a constructor (in fact, you cannot provide one), even if the superclass does not have a default constructor. This case (described in section 15.9.5.1 of the Language Language Sepcification section ) is to create an anonymous subclass of the class using a non-standard constructor; the compiler will automatically create a constructor with the correct arguments and call the corresponding (non-standard) superclass constructor. For instance:

 class X { public X(int value) { ... } // no default constructor! public void foo() { ... } } X myX = new X(3) { @Override public void foo() { ... } }; 

Then myX will be an instance of an anonymous subclass of X with a constructor generated by the compiler that takes an int argument and calls super(intArg) .

+12
source

If the default superclass constructor accessed via super () is available, then the subclass should not have an explicit constructor; it will automatically get the default argument constructor. If a superclass has explicit constructors, all of which accept arguments, then the subclass also needs an explicit constructor, since without such a constructor there would be no way to find out with what arguments the superclass constructor should be called.

+2
source

If a superclass does not have a default constructor (i.e. not args), then you must define a constructor that invokes a particular superstructor.

If the superclass has a default constructor, you do not need to declare a constructor, because the following constructor will be implicitly defined for you if you do not declare any constructors:

 SubClass() { super(); // Note: the no-args super constructor may itself be implicit } 

So, in this case, you do not need to declare the constructor in the subclass.

+2
source

If the default constructor is available in the superclass, then the compiler will include a call to the default constructor in the default constructor for the helper class. For instance:

 class Base(){ Base(){ // default constructor ... } } class Sub extends Base{ // no constructor } 

In this case, not required.

 class Base(){ Base( int i){ // no default constructor ... } } class Sub extends Base{ // no constructor } 

In this case, it is necessary.

 class Sub extends Base{ Sub(){ Base(1); } } 
0
source

All Articles