Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

java - 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));
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...