Skip to content
Related Articles

Related Articles

Inheritance and Constructors in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 13 May, 2022

Constructors in Java are used to initialize the values of the attributes of the object serving the goal to bring Java closer to the real world. We already have a default constructor that is called automatically if no constructor is found in the code. But if we make any constructor say parameterized constructor in order to initialize some attributes then it must write down the default constructor because it now will be no more automatically called.   

Note: In Java, constructor of the base class with no argument gets automatically called in the derived class constructor. 

Example:

Java




// Java Program to Illustrate
// Invocation of Constructor
// Calling Without Usage of
// super Keyword 
 
// Class 1
// Super class
class Base {
 
    // Constructor of super class
    Base()
    {
        // Print statement
        System.out.println(
            "Base Class Constructor Called ");
    }
}
 
// Class 2
// Sub class
class Derived extends Base {
 
    // Constructor of sub class
    Derived()
    {
 
        // Print statement
        System.out.println(
            "Derived Class Constructor Called ");
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of sub class
        // inside main() method
        Derived d = new Derived();
 
        // Note: Here first super class constructor will be
        // called there after derived(sub class) constructor
        // will be called
    }
}
Output
Base Class Constructor Called 
Derived Class Constructor Called 

Output Explanation: Here first superclass constructor will be called thereafter derived(sub-class) constructor will be called because the constructor call is from top to bottom. And yes if there was any class that our Parent class is extending then the body of that class will be executed thereafter landing up to derived classes. 

But, if we want to call a parameterized constructor of the base class, then we can call it using super(). The point to note is base class constructor call must be the first line in the derived class constructor. 

Implementation: super(_x) is the first line-derived class constructor.

Java




// Java Program to Illustrate Invocation
// of Constructor Calling With Usage
// of super Keyword
 
// Class 1
// Suoer class
class Base {
    int x;
 
    // Constructor of super class
    Base(int _x) { x = _x; }
}
 
// Class 2
// Sub class
class Derived extends Base {
 
    int y;
 
    // Constructor of sub class
    Derived(int _x, int _y)
    {
 
        // super keyword refers to super class
        super(_x);
        y = _y;
    }
 
    // Method of sub class
    void Display()
    {
        // Print statement
        System.out.println("x = " + x + ", y = " + y);
    }
}
 
// Class 3
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating object of sub class
        // inside main() method
        Derived d = new Derived(10, 20);
 
        // Invoking method inside main() method
        d.Display();
    }
}
Output
x = 10, y = 20

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!