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:
- If the class has no super class, extend it to Object class
- If the class has no constructor, add a default no-parameter constructor
- 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;
}
}
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.9Note the final paragraph.
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()
.All Java classes that don't have any explicit superclass, extend from
Object
, except forObject
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 ofObject
. 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 thenew
operator is called.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, thehashCode()
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 thesuper()
orthis()
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.