Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it unnecessary to put super() in constructor?

People also ask

Is Super needed in constructor?

Through super, we can call the other constructor from within the current constructor when needed. If you are thinking why it's there for a class that is not extending any other class, then just remember every class follows object class by default. So it's a good practice to keep super in your constructor.

What happens when you don't use super () constructor?

If we call "super()" without any superclass Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java. If you call "super()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing).

Why do we write super in constructor?

The Sun compiler says, call to super must be first statement in constructor . The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() .

Where super () can be used within a constructor?

super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.


Firstly some terminology:

  • No-args constructor: a constructor with no parameters;
  • Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and
  • Default constructor: the public no-args constructor added by the compiler when there is no explicit constructor in the class.

So all classes have at least one constructor.

Subclasses constructors may specify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified.

For example:

public class Base { }
public class Derived extends Base { }

This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.

public class Base { }
public class Derived extends Base { public Derived(int i) { } }

Also fine.

public class Base { public Base(String s) { } }
public class Derived extends Base { }

The above is a compilation error as Base has no default constructor.

public class Base { private Base() { } }
public class Derived extends Base { }

This is also an error because Base's no-args constructor is private.


If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.

src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html


Calling the no-arguments super constructor is just a waste of screen space and programmer time. The compiler generates exactly the same code, whether you write it or not.

class Explicit() {
    Explicit() {
        super();
    }
}

class Implicit {
    Implicit() {
    }
}

Update (December 2018):

Writing an explicit super() helps navigating the source code in the IDE.

As of December 2018, neither Eclipse nor IntelliJ provide any means of comfortably navigating from the constructor of the derived class to the constructor of the base class.


Default parent constructor is called from child default constructor even if you do not call it.

Main

public class Main {

    public static void main(String[] args) {
        A a = new B();
    }
}

A

public class A {

    public A() {
        System.out.println("A");
    }
}

B

public class B extends A {

    public B() {
        System.out.println("B");
    }
}

Prints

A
B

Any class constructor always calls "super()" if there is not explicitly called super([arguments]), only we keep in mind access of super class constructor while programming... when we not extends any specific class automatic extends java.lang.Object class