There is a code of simple program. In constructor, super()
is called without extends to the super class, I can not understand what will does this in this situat
There is not need to add
super()
because it is by default added.
It will call Object
class's default constructor because in JAVA every class extends Object
by default.
Constructor from your code works the same as:
Student(String name, int rollNum){
this.name = name;
this.rollNum = rollNum;
}
In your question super()
is just calling constructor of Object
class.
Every class that doesn't explicitly extend another class implicitly extends java.lang.Object
. So super()
simply calls the no-arg constructor of Object.
Note that this explicit call is unnecessary since the compiler would add it for you. You only need to add a super()
call in a constructor when you want to invoke a superclass constructor with arguments.