Is It Unnecessary to Put Super() in Constructor

Is it unnecessary to put super() in 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.

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.

What is the difference between a constructor having super() and not having super() function call in a Java Class

If there is no explicit call to a super class constructor, the compiler will generate a call to super(). Because of this there is no difference between your examples.

Super Function Java with no parameters in superclass

From the language spec:

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.

This means that, if you don't have an explicit call or does have an explicit call to super(), the superclass needs to have a no-arg constructor - either an explicitly declared one, or a default constructor (a no-arg constructor automatically added by the compiler if the class has no other constructors).

As such, if the code compiles with the explicit super(), that means the super class does have a no-arg constructor: and so it would also compile and work equivalently if you omitted the super().

What does calling super() in a React constructor do?

super() is called inside a react component only if it has a constructor. For example, the below code doesn't require super:

class App extends React.component {
render(){
return <div>Hello { this.props.world }</div>;
}
}

However if we have a constructor then super() is mandatory:

class App extends React.component {
constructor(){
console.log(this) //Error: 'this' is not allowed before super()

}
}

The reason why this cannot be allowed before super() is because this is uninitialized if super() is not called. However even if we are not using this we need a super() inside a constructor because ES6 class constructors MUST call super if they are subclasses. Thus, you have to call super() as long as you have a constructor. (But a subclass does not have to have a constructor.)

We call super(props) inside the constructor if we have to use this.props, for example:

class App extends React.component{
constructor(props){
super(props);
console.log(this.props); // prints out whatever is inside props

}
}

I hope I could make it clear.

super-constructor if there is no super class?

By default all classes inherit java.lang.Object. So a hidden code in your class is

public class Computer extends java.lang.Object implements Serializable {

private static final long serialVersionUID = 1L;

//...
public Computer() {
super(); //here Object's default constructor is called
}
//...
}

If a parent class has default constructor (no argument) and if a child class defines a default constructor, then an explicit call to parent's constructor is not necessary.

Java does it implicitly, in other words, Java puts super() before first statement of the child's constructor

consider this example

class Base {

public Base() {
System.out.println("base");
}
}

class Derived extends Base {

public Derived() {
//super(); call is not necessary.. Java code puts it here by default
System.out.println("derived");
}
}

So when you create Derived d = new Derived(); the output is..

base
derived


Related Topics



Leave a reply



Submit