Search code examples
javasuperdefault-constructor

why does the default constructor in MyClass calls super i.e of Object class


I've been working with java and learning i've a question regarding default constructor in a class. Why does it call super (constructor of Object class.I know it does constructor chaining)?. For what reasons it is required ?. If I define a class like this

MyClass 
{

    public MyClass()
       {

       }
}

the compiler adds super in the constructor.

public MyClass()
{
   super();  
}

P.S I've tried googling and have read Oracle Doc but couldnt find the answer .why? Thanks for your time.


Solution

  • Every constructor must call either a different constructor of the same class or a constructor of its direct super class. The call to the super class constructor is added implicitly if you don't call it explicitly.

    Since an instance of a class inherits the state of its ancestors, it must initialize it by calling the constructors of its ancestors.

    In your case, your MyClass is a direct sub-class of Object, so your constructor must call the constructor of Object.