-1

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;
        }
}
yoges nsamy
  • 1,275
  • 1
  • 16
  • 29
  • There is literally no reason to ever write `extends Object` in Java. – M. Prokhorov Jun 23 '17 at 09:38
  • @M.Prokhorov agree. The above code is shown by the instructor to illustrate how the compiler works. – yoges nsamy Jun 23 '17 at 09:40
  • Also, this: `If the class has no constructor, add a default no-parameter constructor` is a wrong rule of thumb as well. – M. Prokhorov Jun 23 '17 at 09:40
  • 1
    You can refer [this](https://stackoverflow.com/questions/3767365/super-in-java). Hope this will help you. – Nishant Bhardwaz Jun 23 '17 at 09:40
  • @M.Prokhorov could you elaborate on why the rule is wrong please? – yoges nsamy Jun 23 '17 at 09:42
  • @misaikucing, I will actually back off from that last statement, I was not realizing that you were talking about compiler. But then, if we talk about compiler, then the first thing it actually does in constructor is it loads a `this` variable (you can open any compiled class file and see for yourself) – M. Prokhorov Jun 23 '17 at 09:47
  • @misaikucing Because it doesn't apply to `java.lang.Object`. – user207421 Jun 23 '17 at 09:48
  • @M.Prokhorov Not in the case of `java.lang.Object`. All the default constructor contains is a `return` opcode. – user207421 Jun 23 '17 at 09:49
  • @EJP, I'm pretty sure it almost does, except Object actually does have a constructor (defined by JVM as a special case), so compiler will not need to add it. – M. Prokhorov Jun 23 '17 at 09:49
  • @EJP but I see that the Object class does have an empty constructor though https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html – yoges nsamy Jun 23 '17 at 09:49
  • @M.Prokhorov Exactly as I said. The constructor for `java.lang.Object` is not 'defined by the JVM as a special case.` Time for you to stop guessing. – user207421 Jun 23 '17 at 09:50
  • @EJP, I would disagree that rule is not applied to `Object` - the rule action is *not applicable* to Object, however, because rule predicate does not hold. – M. Prokhorov Jun 23 '17 at 09:53
  • @M.Prokhorov I don't know what 'rule' or 'predicate' you are talking about, or what the sophistical difference between 'applied' and 'applicable' might be, or who used any of these terms in the first place. It certainly wasn't me. You seem to be confused about what I'm referring to. It is your claim that 'the first thing it actually does in constructor is it loads a `this` variable'. This is untrue, and that is verifiable, and explicable by reference to the JLS #8.8.9. Not 'defined by the JVM'. – user207421 Jun 23 '17 at 10:03
  • @EJP, I'm looking at compiled bytecode right now, and I see there: `0 aload_0 [this] 1 invokespecial java.lang.Object() 4 return`. – M. Prokhorov Jun 23 '17 at 10:09

3 Answers3

2

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.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
1

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.

Alex Roig
  • 1,534
  • 12
  • 18
  • 'All Java classes that don't have any explicit superclass extend from `Object`', *except for `Object` itself.* – user207421 Jun 23 '17 at 09:59
0

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().

davidxxx
  • 125,838
  • 23
  • 214
  • 215