6

I found a class like this:

public class Computer implements Serializable {

        private static final long serialVersionUID = 1L;    

        //...
        public Computer() {
            super();
        }
        //...
}

Can someone explain me, how this works? The class isn't inheriting any super class and there could still be "super();" in the constructor?

codepleb
  • 10,086
  • 14
  • 69
  • 111
  • 3
    You [inherit](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) from the Object class, which has a no-arg constructor. _"In the absence of any other explicit superclass, every class is implicitly a subclass of Object."_ – Alexis C. Jan 17 '14 at 08:29
  • 1
    Note that a call to a no-arg `super()` is unnecessary; it will be inserted by the compiler if you don't specify it. – chrylis -cautiouslyoptimistic- Jan 17 '14 at 08:34
  • java.lang.Object is the only class without a superclass, and its constructor does not contain a super() call. – Patricia Shanahan Jan 17 '14 at 08:37
  • 1
    I should have known this. Never imagines that someone would want to call the "Object"'s constructor. Thanks to you all. :) – codepleb Jan 17 '14 at 08:44
  • Could be the result of a coding standard, some of which mandate explicit coding of implicit features, like calling super(), explicit public modifiers on interface elements, implicit private modifier on enum constructors, always prefixing instance member access with this and class member access with class name, and so on. It's not necessarily a good idea, IMO. – MrBackend Jan 17 '14 at 08:57
  • See also http://stackoverflow.com/questions/2490875/object-class-as-super-class-in-java – Raedwald Jan 17 '14 at 12:41
  • See also http://stackoverflow.com/questions/8669693/advantage-of-singly-rooted-class-hierarchy – Raedwald Jan 17 '14 at 12:50
  • Does this answer your question? [Calling super() with no Superclass?](https://stackoverflow.com/questions/13572468/calling-super-with-no-superclass) – Federico Baù Jun 05 '23 at 06:52

5 Answers5

8

By default all classes inherit java.lang.Object. So a hidden code in your class is

public class Computer extends java.lang.Object implements Serializable {

        private static final long serialVersionUID = 1L;    

        //...
        public Computer() {
            super(); //here Object's default constructor is called
        }
        //...
}

If a parent class has default constructor (no argument) and if a child class defines a default constructor, then an explicit call to parent's constructor is not necessary.

Java does it implicitly, in other words, Java puts super() before first statement of the child's constructor

consider this example

class Base {

    public Base() {
        System.out.println("base");
    }
}

class Derived extends Base {

    public Derived() {
        //super(); call is not necessary.. Java code puts it here by default
        System.out.println("derived");
    }
}

So when you create Derived d = new Derived(); the output is..

base
derived
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Ah yeah, I know this but didn't imagine that someone would call the constructor of "Object". Does this have a reason? Thanks for your answer! :) – codepleb Jan 17 '14 at 08:42
  • 1
    Its mere a coding style.. if you see `Object` class has not defined a constructor - which is equivalent to defining a default constructor with empty body.. in other words, calling `super()` in your case does not yield anything – sanbhat Jan 17 '14 at 08:46
4

When you do not extends any class then by default the inherited class is Object so it calls Object class constructor.

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
2

In Java, Object class is at the top of the Class hierarchy. Implicitly, All the Classes are implicitly of Object type. Invocation of super() method is not needed explicitly in code. But, this will be added by the compiler during conversion of Class into bytecode. so, the first line in any constructor would be invocation of super() implicitly by default. Construction of an object includes all the invocation of default constructors of Super classes. But, you can also call a customized argument constructor of the super class in your class constructor.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1

By default every java class implicitly inherits java.lang.Object class. So, super() will call the Object class constructor in your case!

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

By default any class even if we don't extend any super class, extends java.lang.Object class. So it calls Object constructor.

By the inheritance concept, we can have following code compiled properly.

Object obj = new Computer();

Even, in fact, your interface reference can be assigned to java.lang.Object reference.

interface ICommand 
{

}
class Computer implements ICommand
{

}
class Test
{
    public static void main(String[] arg)
    {
        ICommand ic = new Computer();
        Object obj = ic;  // 'ic' is an interface reference
    }
}

by this way, java.lang.Object is also super interface (this is not exact technical term ) for all interfaces.

AmitG
  • 10,365
  • 5
  • 31
  • 52