This article summarizes the five most common problems with Java constructors!

Why is the constructor of a subclass called by default

Look at this simple example:

package cc;

public class Sub extends Super {
	public Sub() {
		System.out.println("Sub");
	}
	
	public static void main(String[] args) {
		Sub sub = new Sub();
	}
}

class Super {
	public Super() {
		System.out.println("Super"); }}Copy the code

When inherited from a class, the constructor first calls the super() method. If the statement is not explicitly written, the compiler inserts it automatically. That’s why our example program calls the super constructor first. Remember, though, that ** creates only one object, the child, even though it calls the parent constructor. ** Calls the constructor of the parent class because the super class may need constructors to initialize some private member variables. After the super constructor is automatically inserted by the compiler, the subclass’s constructor looks like this:

public Sub(){
    super();
    System.out.println("Sub");
}
Copy the code

Implicit super constructor is undefined for default constructor. Must define an explicit constructor

Implicit super constructor is undefined for default constructor. Must define an explicit constructor

This error is a common one encountered by many developers because the default constructor in the superclass cannot be found.

Look at the following code:

package cc; public class Sub extends Super { public Sub(String s) { } public static void main(String[] args) { Sub sub = new Sub(); } } class Super { String s; public Super(String s) { this.s = s; }}Copy the code

Implicit super constructor super () is undefined. Must explicitly invoke another constructor.

The compiler error is because the default super() no-argument constructor is undefined. In Java, if a class does not define a constructor, the compiler automatically inserts a default constructor with no arguments. However, if a constructor is defined in a class, the compiler will not automatically insert a constructor with no parameters, so if we do not display a constructor with no parameters, the constructor does not exist.

In the previous section, we saw that if a subclass’s constructor does not explicitly call the parent class’s constructor, then the compiler inserts super(), which automatically calls the no-argument constructor. But in this case, the parent class has no parameterless constructor, so an error is reported.

To solve this problem, we can simply insert a constructor with no arguments into the parent class, or the parent class of the call shown in the subclass constructor has arguments.

Constructor that calls the parent class as shown in the constructor of a subclass

The following code is correct.

Rules for using constructors

Simply put, when used, the constructor of a subclass must call the constructor of the parent class, regardless of whether the declaration is displayed. So, the parent constructor to be called must be defined!

Why doesn’t Java implement the default no-argument constructor when a class already implements one?

That’s an interesting question. We know that if we do not declare a constructor in a class, the compiler will implicitly implement a constructor with no arguments, but if we do declare a constructor with or without arguments, the compiler will not provide the default constructor, so why do we do this?

For one thing, if we were to automatically implement a constructor with no arguments for all classes, we might have a problem breaking class design principles. For example, consider the Scanner class, he has a few constructor, you can use these constructors, state that you want to read the data source, if the compiler increase without no-parameter constructor, then you don’t read given data source, will be an error, program cannot execute, because we have to specify a data source will let him go to read the data.