问题描述:

If we, in our program, have just one class, without extending any class. For example

public class Point {

int x, y;

}

Compiler creates default constructor and call the super() method acording to this http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9

public class Point {

int x, y;

public Point() {

super();

}

}

Q: As i understand super(); is calling default constructor of super class, but in this case we do not have a super class, so what is super() calling in that case?

网友答案:

All java classes extend from Object

网友答案:

You do have a super class. All classes in Java automatically extend java.lang.Object, regardless of whether you specify it or not.

See here: http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html

To take one snippet from that link:

All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.

网友答案:

The default contructor is Object, which all Java objects inherit from

网友答案:

In java, every class has a super class. If none is explicitly given, then it's Object

网友答案:

All Java Classes extends from Object, so if you are not extending any class, by super you are calling constructor of Object class.

网友答案:

Object is the super type of all in java. Super() it will call Object class.

网友答案:

Each class in Java implicitly extends Object Class. So you can always call super() from the constructor of any class.

Again in Object class there is no explicit constructor. Compiler creates a default one and the default constructor of the Object class creates the object itself.

相关阅读:
Top