Using the super Keyword to Call a Base Class Constructor in Java

We prefer inheritance to reuse the code available in existing classes. In Java, Inheritance is the concept in which one class inherits the properties of another class. In the below example there are two classes Programming and DP while Programming is Parent class and DP is child class. From the main class, we have created an object of DP i.e. child class as it also allows us to access the methods from its parent class, but if we create an object of Parent class(Programming) then we cannot access the methods or objects from its child class.

After creating an object of child class we have first called a method from child class and then called a method from the parent class. This doesn’t matter as we can call the objects in any order.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate inheritance properties
  
class Programming {
    
    // Creating method m1 for class Programming
    public void m1() 
    
      System.out.println("Programming"); 
    }
}
class DP extends Programming {
    
    // Creating method m2 for class DP
    public void m2() 
    
      System.out.println("DP"); 
    }
}
public class GFG {
    
    public static void main(String[] args)
    {
        // Creating Obj for Class DP and
        // Calling both m1 from class programming
        // And calling m2 from class DP respectively.
        DP obj = new DP();
        obj.m2();
        obj.m1();
    }
}

chevron_right


Output :

DP
Programming

Constructor Inheritance

In the below example, we have created a parent and child class. Here, we have created an object of the child class, as the constructor will call itself on creating an object we need not mention anything.

After creating an object of child class the constructor is expected to print the output from its own class, but from the output, we can identify that Parent class got executed and then child class got executed, this is because we have created a constructor for inherited class and every class contains a super() by default, as we are calling an inherited class it contains super() in its first line and calls the Parent class.



Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to ilustrate
// the concept of Constructor
// inheritance.
  
// Base Class
class Programming {
    
    // Constructor for class Programming
    public Programming()
    {
        System.out.println("Programming");
    }
}
  
// Child Class inherit the Base
// Class
class DP extends Programming {
    
    // Constructor for class DP
    public DP() { System.out.println("DP"); }
}
  
// Main Class
public class GFG {
    
    public static void main(String[] args)
    {
        // Ceating obj for
        // class DP
        DP obj = new DP();
    }
}

chevron_right


Output

Programming
DP


Constructor Overloading With Super

In the below example we have used the constructor overloading concept, and we have created an object of child class and after calling the constructor of child class the first line in it is super(10, 20) which says that call the matching constructor from the parent class, if we do not mention that line, by default it calls the super() with no parameterized constructor from Parent class.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the concepts of constructor
// overlaoding.
  
// Base Class
class Programming {
    
    // Creating Constructor for
    // class Programming.
    public Programming()
    {
        System.out.println("Programming");
    }
  
    // Parameterized Constructor
    public Programming(int i, int j)
    {
        System.out.println("Programming + +");
    }
}
  
// Child Class
class DP extends Programming {
    
    public DP()
    {
        // Calling by using
        // Programming(int i,int j)
        // from class Programming.
        super(10, 20);
        System.out.println("DP");
    }
  
    // Parameterized Constructor
    // for class DP
    public DP(int i, int j)
    {
        System.out.println("DP + +");
    }
}
  
// Main Class
public class GFG {
    public static void main(String[] args)
    {
        // Creating Obect for class DP.
        DP obj = new DP();
    }
}

chevron_right


Output:

Programming + +
DP

Analogy

Let’s look at the below snippet,

  • In the below code we have created an object of the child class, and we are passing the value of 10 from the object line itself and after going to the specific constructor it first calls super() by default and prints “Programming” from the Parent class.
  • The point to note is here we are calling a parameterized constructor from the object creation line but it will call super() by default as will be available by default.
  • In child class, we can also give super() with parameters to call a specific constructor from Parent class.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Base Class
class Programming {
    
    // Default Constructor
    public Programming()
    {
        System.out.println("Programming");
    }
  
    // parameterized Constructor
    public Programming(int i, int j)
    {
        System.out.println("Programming + +");
    }
}
class DP extends Programming {
    
    public DP() { System.out.println("DP"); }
  
    // parameterized Constructor with
    // one parameter
    public DP(int i) { System.out.println("DP +"); }
  
    // parameterized Constructor with
    // two parameter i and j.
    public DP(int i, int j)
    {
        System.out.println("DP + +");
    }
}
  
// Main Class
public class GFG {
    
    public static void main(String[] args)
    {
  
        // Creating obj for DP
        // class which inherits the
        // properties of class programming
        DP obj = new DP(10);
    }
}

chevron_right


Output :

Programming
DP +

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.