Java Program to Illustrate the Availability of Default Constructor of the Super Class to the Sub Class by Default


Here, we will demonstrate the availability of the default constructor of the super class to the sub class by default through Java Program.

Before diving deep into the topic, let us get acquainted with the term Constructor, Super class, and subclass.

Constructor

A special method in Java is considered for the initialization of an object. The name of the constructor is the same as the class name and it does not return anything.

A default constructor is invoked by itself whenever an object is created using a new keyword.

There are following three types of constructors in Java

  • Default Constructor.

  • Parameterized Constructor.

  • Copy Constructor.

Here, we will deal with the Default constructor only.

A default constructor is one having no parameters and it initializes the data members by default values such as 0, 0.0, null, etc. depending on the type of data provided.

Superclass and subclass

The class which serves as a base for the derived class is termed a Superclass while the class which derives or inherits from the base class is termed a Subclass. It is inherited with the help of the extend keyword.

Example

In the following Java program, it is illustrated how the inheritance mechanism in Java and the default constructors work in parent and child classes. It also shows how the default constructor of the parent class is automatically called by the child class constructor when an object of the child class is created.

// Java Program to demonstrate the Availability of Default
import java.io.*;
// creation of parent class named A
class A {
   // default constructor of class A
   A () {
      System.out.println(
         "This is a default constructor of class A");
   }
}
// creation of a child class named B and inheriting parent class A to the
// child class B
class B extends A {
   // default constructor of child class B
   B () {
      System.out.println(
         "This is a default constructor of class B");
   }
}
public class TutorialsPoint1 {
   public static void main (String [] args) {
      // creation of an object of parent class A that will
      // only invoke the default constructor of the parent class
      A o1 = new A ();
      // creation of an object of child class B that will
      // invoke a constructor of parent class A first and then
      // the constructor of the child class
      B o2 = new B ();
   }
} 

Output

This is a default constructor of class A
This is a default constructor of class A
This is a default constructor of class B

In the above program, a parent class named A is defined with a default constructor that simply displays a message when it is invoked.

A child class named B is also defined here which extends A and has its own default constructor that also displays a message when gets invoked.

In the main method of the program, two objects are created. The first object is created by means of the default constructor of class A, which only displays the message defined in the parent class. The second object is created using the default constructor of class B, which first invokes the constructor of class A to display its message, and then invokes the constructor of class B to display its message.

Example

In the following Java Program, the concept of constructor chaining is demonstrated. It is shown how the default constructors are automatically invoked by the child class constructors as soon as an object of the child class is created. It also shows how the constructors of the parent classes are invoked in a hierarchical manner from top to bottom.

// Java Program to demonstrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class A {
   // default constructor of the class named A
	A() { System.out.println("\n This is a default constructor of class A "); }
}
class B extends A {
   // default constructor of the class named B
	B() { System.out.println("\n This is a default constructor of class B "); }
}
class C extends B {
   // default constructor of the class named C
	C() { System.out.println("\n This is a default constructor of class C "); }
}
public class TutorialsPoint2{
	public static void main(String[] args){
	   // creation of an object of class C
		// that will invoke the constructor of C
		// but before invoking the constructor of class C
		// the constructor of its parent
		// class which is B will get invoked but C is a child of class A Thus,
		// before invoking the constructor of class B, the constructor of the 
		// class A that is the parent of
		// class B shall get invoked
		C o = new C ();
	}
}

Output

This is a default constructor of class A 
This is a default constructor of class B 
This is a default constructor of class C 

In the above program, three classes namely A, B, and C are defined. Class A has a default constructor that simply displays a message when it is invoked. Class B extends A and has its own default constructor that also displays a message when it is invoked. Class C extends B and has a default constructor that displays a message when it is invoked.

In the main method of the program, an object of class C is created. After the constructor of class C is invoked, it first invokes the constructor of its immediate parent class B to display its message, and then invokes the constructor of class A, the parent of class B, to display its message.

Lastly, the constructor of class C itself is invoked to display its message.

Example

The following Java program demonstrates the usage of the super () keyword in Java to invoke the constructor of a parent class and how constructor chaining works in a class hierarchy with multiple levels of inheritance. The super() keyword can be used to invoke either the default or parameterized constructors of the parent class.

// Java Program to demonstrate the Availability of DefaultvConstructor of the Super Class to the Sub Class by Default
import java.util.*;
class A {
   // default constructor of a class named A
	A () { System.out.println("This is a default constructor of class A "); }
}
class B extends A {
	// default constructor of a class named B
	B ()	{
	   // java compiler invokes keyword super () by
		// default in the case of the default constructor
		super ();
		System.out.println("This is a default constructor of class B ");
	}
}
class C extends B {
	// default constructor of a class named C
	C (){
		//  java compiler invokes the keyword super()  by
		// default in the case of the default constructor
		super ();
		System.out.println("This is a default constructor of class C ");
	}
}
public class TutorialsPoint3 {
	public static void main(String[] args){
	   // creation of an object of class C
		// that will invoke the constructor of C
		// but before invoking the constructor of class C
		// the constructor of its parent
		// class shall be invoked which is B but B is a child of A class so,
		// before invoking the constructor of the B class, it
		// will invoke the constructor of A class that is the parent of
		// class B
		C obj = new C ();
	}
}

Output

This is a default constructor of class A 
This is a default constructor of class B 
This is a default constructor of class C 

In the above program, three classes namely A, B, and C are defined.

Class A has a default constructor that simply displays a message when it is invoked. Class B extends A and has its own default constructor that first invokes the constructor of its parent class A by means of the super () keyword, and then displays its own message. Class C extends B and has a default constructor that first invokes the constructor of its parent class B using the super () keyword, and then displays its own message.

In the main method of the program, an object of class C is created. When the constructor of class C is invoked, it first invokes the constructor of its immediate parent class B using the super () keyword to display its message, which in turn invokes the constructor of class A using the super () keyword to display its message.

Lastly, the constructor of class C itself is invoked to display its message.

Conclusion

This article threw light on the methods to demonstrate the availability of the default constructor of the superclass to the subclass by default.

The discussion began with the terms Constructor, superclass, and subclass. Further, three methods and their implementations in Java are discussed to perform the stated operation.

Updated on: 11-Aug-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements