Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super keyword without extends to the super class

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 situation?

public class Student {

    private String name;
    private int rollNum;

    Student(String name,int rollNum){
        super(); //I can not understand why super keyword here.
        this.name=name;
        this.rollNum=rollNum;
    }


    public static void main(String[] args) {

        Student s1 = new Student("A",1);
        Student s2 = new Student("A",1);

        System.out.println(s1.equals(s2));
    }

}
like image 998
Roledenez Avatar asked May 03 '14 07:05

Roledenez


2 Answers

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.

like image 92
JB Nizet Avatar answered Oct 12 '22 00:10

JB Nizet


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.

like image 26
Braj Avatar answered Oct 12 '22 02:10

Braj