Derived Class Constructors in Java

Introduction 

Constructors are used to initialize an object of a particular type, as well as to allocate memory, and have the same name as the class. They are automatically called when the object is created. Similarly, Constructors of a subclass have the same name as the subclass and are called when an object of the subclass is created. The important point to be noted is that the base class constructors are never inherited in the derived class. All methods and properties of the base class are inherited by objects of the derived class, except the constructors.

When an object of subclass is created, it contains within it all the methods and properties of the base class. It is very important that the base class values should be initialized correctly. This can only be done if the base class constructor is called from within the derived class constructor. If no constructors are provided in the base class, Java automatically inserts a call to the base class constructor in the derived class default constructor when an object of the derived class is created. The important point to be noted is that the base class constructors are always called before the derived class constructors are called. The source code illustrates this point.

class Author {
    Author() {
        System.out.println("In the Author class");
    }
}

class FictionAuthor extends Author {
    FictionAuthor() {
        System.out.println("In the Fiction Author class");
    }
}

class TestAuthor {
    TestAuthor() {
    }

    public static void main(String[] args) {
        FictionAuthor ficauthor = new FictionAuthor();
    }
}

Output

Derived Class Constructors in Java

This example consists of two classes. The class Author is the base class, and the class FictionAuthor is the derived class. In both classes, a constructor has been declared. When an object of the derived class is created, it first calls the constructor of the superclass. Then it invokes the constructor of the derived class, as seen from the output shown .

On the other hand, programmers can also invoke default and parameterized constructors in the base class from the derived class constructor by using the keyword super. The statement that calls the superclass constructor should be the first statement in the subclass constructor. This is because the superclass comes into existence before the subclass. The syntax is:

super(parameter_list) or super() ;

Here, parameter_list specifies one or more parameters needed by the constructor in the superclass. When a subclass constructor calls super (), it is called the constructor of this immediate superclass. The example below demonstrates the usage of the keyword super.

class Author {
        String name;
        Author (String str )  {
                  name = str ;
                  System.out.println ("Printing from the Author class");
                  System.out.println ("The name is " + name );
          }
}
class FictionAuthor extends Author  {
    String storytype;
    FictionAuthor(String name, String type)  {
              super(name);
              storytype=type;
              System.out.println("Printing from the Fiction Author class");
              System.out.println("Type is "+ storytype);
       }
}
class TestingAuthor  {
   TestingAuthor()   {
   }
public static void main(String [ ] args )   {
          FictionAuthor ficauthor = new FictionAuthor ("Ashish Bhatnagar" , "Novel");
     }
}

Output

Derived Class Constructors in Java

In this source code, it is assumed that there are two types of authors-Authors and FictionAuthors. For both types of authors, name is required and stored in the variable named name declared in the class Author. In the FictionAuthor class, which is derived from the Author class, additional information about the type of book is required and stored in the variable storytype. The storytype can have two values - either it can be ShortStory or it can be a Novel. Depending on the type of value passed to the constructor when the object is created, an output is generated.

Summary 

In Java, constructors are used to initialize objects and allocate memory, sharing the same name as the class. When an object of a subclass is created, it inherits all methods and properties from the base class, except constructors, which are not inherited. However, the base class constructor must be invoked from the subclass constructor to ensure proper initialization of the base class values. If no constructor is explicitly provided in the base class, Java automatically calls the base class constructor when creating an object of the derived class. Crucially, base class constructors are always executed before subclass constructors.