Java Program to Illustrate the Availability of Default Constructor of the Super Class to the Sub Class by Default
Last Updated :
11 Jan, 2022
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
import java.io.*;
class defaultConstructor {
int a;
double d;
String s;
defaultConstructor()
{
System.out.println( "Hi I am a default constructor" );
}
}
class GFG {
public static void main(String[] args)
{
defaultConstructor obj = new defaultConstructor();
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
import java.io.*;
class parent {
parent()
{
System.out.println(
"I am default constructor from parent class" );
}
}
class child extends parent {
child()
{
System.out.println(
"I am default constructor from child class" );
}
}
class GFG {
public static void main(String[] args)
{
parent obj1 = new parent();
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
import java.util.*;
class z {
z() { System.out.println( "Hi I am z" ); }
}
class y extends z {
y() { System.out.println( "Hi I am y" ); }
}
class x extends y {
x() { System.out.println( "Hi I am x" ); }
}
class GFG {
public static void main(String[] args)
{
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
import java.util.*;
class z {
z() { System.out.println( "Hi I am z" ); }
}
class y extends z {
y()
{
super ();
System.out.println( "Hi I am y" );
}
}
class x extends y {
x()
{
super ();
System.out.println( "Hi I am x" );
}
}
class GFG {
public static void main(String[] args)
{
x obj = new x();
}
}
|
Output
Hi I am z
Hi I am y
Hi I am x
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...