-->

What does the default constructor in the class OBJ

2019-03-07 02:23发布

问题:

This question already has an answer here:

  • Why call super() in a constructor? 5 answers

I'm a Java beginner learning about Java compiler rules as below:

  1. If the class has no super class, extend it to Object class
  2. If the class has no constructor, add a default no-parameter constructor
  3. If the first line of the constructor is not "super()" or "this()", add "super()" to call the default constructor of the super class.

I understand that all objects we create are derived from the super class Object. My question is what does the constructor in the Object class do when called?

Edit: My question is about what the constructor does in the class Object? I'm aware that subclasses call superclass's constructor by default.

For example, I have the following code (where I explicitly extend to Object and call super() to illustrate what the compiler does). My question is, what does the call to super() do?

public class Person extends Object
{
    private String name;
    public Person(String n)
        {   
            super();
            this.name = n;
        }
}

回答1:

My question is, what does the call to super() do?

It calls the default constructor for java.lang.Object. And to answer what you seem to be really asking, from the Java Language Specification, #8.8.9

8.8.9. Default Constructor

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

The default constructor has the same accessibility as the class (§6.6).

The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).

The default constructor has no throws clauses.

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

Note the final paragraph.



回答2:

All Java classes that don't have any explicit superclass, extend from Object, except for Object itself. There's no need to make it explicit in your code, the compiler will take care of.

Also, the super() call in the constructor will invoke the constructor of Object. If you look at the implementation, you'll see that Object doesn't have any constructor at all, so it'll use an implicit empty constructor which does nothing. Details such as constructing a vtable to make sure that the inherited methods are there to use or initialising the object's monitor are not part of the constructor but are performed by the JVM implementation when the new operator is called.

public Object() {

}

The Object class has few methods that come handy in most of the classes. For example, the infamous toString() is implemented in the Object class. Also, the hashCode() is implemented there.

I'd recommend you to have a deep look at the Object.class file to understand what methods are inherited in every single Java class.

Regarding your 3 rules at the top, are "good" for academic purposes but never used in reality. You'll never see an explicit extends Object, and only call the super() or this() when you need to overwrite the default behaviour.

Also, avoid abusing of inheritance and use more composition (implement an interface), although, it depends on the every use case.



回答3:

For example, I have the following code (where I explicitly extend to Object and call super()). My question is, what does the call do super() do?

Actually, Object() does nothing.
Now you should not reduce the constraint to invoke super() (with args or not) for any subclass constructor to the case where you have a class that doesn't extend a class explicitly.

This case is the only one where invoking super() may seem not really useful.
But as soon a class extends a class distinct from Object, this constraint makes really sense as the child construction has to apply first its parent construction.

But the language specifies this point in a general way. It has no exception for classes that extend directly Object.
Probably because, it makes things more simple and that is not a real constraint as the compiler adds for you the call to the super default constructor : super().