Does a subclass NEED to have a constructor?

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

Regarding calling super(): the first thing every constructor must do is either call a different constructor in the same class by calling this() (possibly with some arguments) or call a constructor of its superclass by calling super() (again, possibly with arguments). Neither of those calls can go anywhere else. If a constructor doesn't start with either one, the compiler will automatically insert a call to super() (without any arguments). So if the behavior you want is to call through to the default superclass constructor (and, many times, it is) then you don't need to explicitly call super() yourself.

There's also one situation where you do not need to provide a constructor (in fact, you can't provide one) even when the superclass has no default constructor. This case (described in section 15.9.5.1 of the Java Language Sepcification) is when you create an anonymous subclass of a class using a non-default constructor; the compiler will automatically create a constructor with the correct arguments and call up to the corresponding (non-default) 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 compiler-generated constructor that takes an int argument and calls super(intArg).

Because you cannot write a constructor for anonymous classes, there's an issue: what if you need to do some object initialization when the object is created? The solution is to use an instance initializer block. For example:

X myX = new X(3) {
    // Field unique to this subclass of X:
    private int baz;
    {
        // code here runs as if it were at the start of every constructor
        baz = ...;
    }
    @Override
    public void foo() { ... }
};

If the super class does not have a default (ie no args) constructor, then you must define a constructor that calls a specific super constructor.

If the super class does have a default constructor, you don't have to declare a constructor, because the following constructor will be implicitly defined for you if you don't declare any constructors:

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

So in that case, you don't have to declare a constructor in the sub class.


If the default superclass constructor accessed through super() is available, then the subclass does not need to have an explicit constructor; it will automatically get a default no argument constructor. If the superclass has explicit constructors all of which take arguments, then the subclass needs an explicit constructor as well, since without such a constructor there would be no way to know what arguments the superclass constructor should be called with.