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
// 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);
}
}
OutputHi 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();
}
}
OutputI 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();
}
}
OutputHi 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();
}
}
OutputHi I am z
Hi I am y
Hi I am x
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read