Open In App

Inheritance and Constructors in Java

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 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 Program to Illustrate Invocation
// of Constructor Calling With Usage
// of super Keyword
 
// Class 1
// Super 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

 


Article Tags :