Why Call Super() in a Constructor

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

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.

Calling Super class Constructor

A few problems with your code.

First, as the error suggest, you cannot use the member variable before the super call because the object has not been fully initialized yet. Second, your subclass shouldn't have same public variables as the superclass. Third, a common practice in Java is to use getter/settter instead of public variables.

Here is my version of your code:

public class GregorianDate extends Date {
//Define constants
private final static int MONTH = 1;
private final static int DAY = 1;
private final static int YEAR = 1970;

//*************** Constructors ***********************
GregorianDate() {
super(MONTH,DAY,YEAR);
long numToAdd = System.currentTimeMillis();
numToAdd += java.util.TimeZone.getDefault().getRawOffset();
numToAdd /= 86400000;
super.addDays(numToAdd);
}

//Parameterized constructor
GregorianDate(int passedYear, int passedMonth, int passedDay){
super(passedMonth, passedDay, passedYear);
}

//getters and setters here
}

SuperClass file:

public class Date {
private int month;
private int day;
private int year;

Date(int passedMonth, int passedDay, int passedYear){
this.month = passedMonth;
this.day = passedDay;
this.year = passedYear;
}

//Getters and setters here
}

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.

Why do we call super() in constructor

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



Related Topics



Leave a reply



Submit