When Do You Need to Explicitly Call a Superclass Constructor

When do you need to explicitly call a superclass constructor?

You never need just

super();

That's what will be there if you don't specify anything else. You only need to specify the constructor to call if:

  • You want to call a superclass constructor which has parameters
  • You want to chain to another constructor in the same class instead of the superclass constructor

You claim that:

At the same time I've also seen instances on here where someone's problem was not explicitly calling super().

Could you give any examples? I can't imagine how that's possible...

What are the rules for calling the base class constructor?

Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. Unlike Java, C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".

class SuperClass
{
public:

SuperClass(int foo)
{
// do something with foo
}
};

class SubClass : public SuperClass
{
public:

SubClass(int foo, int bar)
: SuperClass(foo) // Call the superclass constructor in the subclass' initialization list.
{
// do something with bar
}
};

More info on the constructor's initialization list here and here.

Why call super() in a constructor?

There is an implicit call to super() with no arguments for all classes that have a parent - which is every user defined class in Java - so calling it explicitly is usually not required. However, you may use the call to super() with arguments if the parent's constructor takes parameters, and you wish to specify them. Moreover, if the parent's constructor takes parameters, and it has no default parameter-less constructor, you will need to call super() with argument(s).

An example, where the explicit call to super() gives you some extra control over the title of the frame:

class MyFrame extends JFrame
{
public MyFrame() {
super("My Window Title");
...
}
}

When is it absolutely necessary to make an explicit call to superclass' parameterized constructors and why so?

Assume that your superclass has a field something and a constructor which initializes that field based on a passed parameter. You cannot create a child instance without properly initializing the super class. And if there is no default constructor the compiler cannot implicitly make the call. Instead it now it needs a parameter value to pass and the compiler cannot figure that value out on its own, instead you as the programmer need to explicitly state which parameter to pass to what super constructor.

Do i need super when creating a subclass or not?

JLS 8.8.7. Constructor Body

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

In

class Base {

Base() {

}
}

public class test2 extends Base {

test2() {

//super();
System.out.print("test2");
}
}

The commented out line is added automatically and since the superclass's no-argument constructor is defined, there is no error.

In the case of

class Quest {

Quest(int y) {

System.out.print("A:"+y);
}
}

class Quest1 extends Quest {

Quest1(int x) {

//super(x+1);
System.out.print("B:"+x);
}
}

The implicit call super() is trying to invoke an undefined constructor in the superclass, which causes the error

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

Uncommenting your explicit constructor call replaces the implicit call and thus the problem is resolved. Alternatively, define a no-argument constructor in the superclass.

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() { ... }
};


Related Topics



Leave a reply



Submit