Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJava Program to Illustrate the Availability of Default Constructor of the Super...

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

A constructor in Java is a special method that is used to initialize an object. Whenever an object is created using the new() keyword at least one construction is called. The constructor name must match with the class name and cannot have a return type. If there is no constructor available in the class in such a case java compiler provides a default constructor(no parameter constructor) by default.

What is the purpose of the default constructor(no-parameter constructor)

The default constructor is used to provide the default values to the object like 0, null, etc depending on the type.

Example of default constructor

In the code given below, we have created a class named default Constructor and inside this class, we have created a default constructor.

Java




// Java program to illustrate default constructor
 
import java.io.*;
class defaultConstructor {
    int a;
    double d;
    String s;
 
    // creating default constructor
    defaultConstructor()
    {
        System.out.println("Hi I am a default constructor");
    }
}
class GFG {
    public static void main(String[] args)
    {
        // creating an object of class defaultConstructor
        // after creating an object it will invoke the
        // default constructor
        defaultConstructor obj = new defaultConstructor();
 
        // default constructor provide default values to
        // the object
        System.out.println(obj.a);
        System.out.println(obj.d);
        System.out.println(obj.s);
    }
}


Output

Hi I am a default constructor
0
0.0
null

The Availability of Default Constructor of the Super Class to the Sub Class by Default.

When we are inheriting from parent to child class, keyword super() has to be called in the child class constructor first. If super() is not called in the child class constructor then the java compiler will do this for us. This is why the parent class constructor is also called whenever we make an object of the child class.

Example 1:

Java




// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
 
import java.io.*;
 
// creating parent class
class parent {
 
    // default constructor of parent class
    parent()
    {
        System.out.println(
            "I am default constructor from parent class");
    }
}
 
// creating child class and inheriting parent class to the
// child class
class child extends parent {
 
    // default constructor of child class
    child()
    {
        System.out.println(
            "I am default constructor from child class");
    }
}
class GFG {
    public static void main(String[] args)
    {
        // creating object of parent class and this will
        // only invoke parent class default constructor
        parent obj1 = new parent();
 
        // creating object of child class and this will
        // invoke parent class constructor first and then it
        // will invoke child class constructor
        child obj2 = new child();
    }
}


Output

I am default constructor from parent class
I am default constructor from parent class
I am default constructor from child class

Example 2:

Java




// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
 
import java.util.*;
 
class z {
 
    // default constructor of class z
    z() { System.out.println("Hi I am z"); }
}
 
class y extends z {
 
    // default constructor of class y
    y() { System.out.println("Hi I am y"); }
}
class x extends y {
 
    // default constructor of class x
    x() { System.out.println("Hi I am x"); }
}
 
class GFG {
 
    public static void main(String[] args)
    {
        // creating an object of class x
        // this will invoke the constructor of x
        // but before invoking the constructor of class x
        // it will invoke the constructor of it's parent
        // class which is y but y is child of z class so,
        // before invoking the constructor of y class it
        // will invoke the constructor of z class(parent of
        // y class)
        x obj = new x();
    }
}


Output

Hi I am z
Hi I am y
Hi I am x

Example 3:

Here, we just want to show the use of the keyword super(), how it works 

Java




// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
 
import java.util.*;
 
class z {
 
    // default constructor of class z
    z() { System.out.println("Hi I am z"); }
}
class y extends z {
 
    // default constructor of class y
    y()
    {
        // keyword super() is called by java compiler by
        // default in case of default constructor
        super();
 
        System.out.println("Hi I am y");
    }
}
class x extends y {
 
    // default constructor of class x
    x()
    {
        // keyword super() is called by java compiler by
        // default in case of default constructor
        super();
        System.out.println("Hi I am x");
    }
}
 
class GFG {
 
    public static void main(String[] args)
    {
        // creating an object of class x
        // this will invoke the constructor of x
        // but before invoking the constructor of class x
        // it will invoke the constructor of it's parent
        // class which is y but y is child of z class so,
        // before invoking the constructor of y class it
        // will invoke the constructor of z class(parent of
        // y class)
        x obj = new x();
    }
}


Output

Hi I am z
Hi I am y
Hi I am x
RELATED ARTICLES

Most Popular

Recent Comments