Skip to content
Related Articles

Related Articles

Improve Article

super keyword for Method Overloading in Java

  • Difficulty Level : Basic
  • Last Updated : 02 Nov, 2020

We use Method overloading to use a similar method for more than one time. This can be achieved by using different parameters in the signature. In the below example a class GFG with three similar methods is available, though the three methods are overloaded they are provided with different parameters. The object of the class GFG calls a method with a String Parameter.

Java




// Java program to demonstrate the Method 
// Overloading without super keyword
  
class GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("No parameter method");
    }
  
    // Method m1 with 1 integer parameter.
    public void m1(int i)
    {
        System.out.println("Int Parameter");
    }
  
    // Method m1 with 1 string parameter.
    public void m1(String s)
    {
        System.out.println("String Parameter");
    }
}
  
// Main Class
public class Main {
    
    public static void main(String[] args)
    {
        // Creating object for GFG class.
        // g is object of GFG class.
        GFG g = new GFG();
  
        // Here, m1 called with string parameter.
        // m1(String s) method will be called.
        g.m1("A");
    }
}

Output :

String Parameter

Using Super Keyword:

If both parent & child classes have the same method, then the child class would override the method available in its parent class. By using the super keyword we can take advantage of both classes (child and parent) to achieve this. 

  • We create an object of child class as it can inherit the parent class methods.
  • In the child class method, we call the method available in its parent class by using super().

Note:



In child class, a super keyword can be given on any line.

Java




class GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("No parameter method");
    }
}
  
// tech class extends class GFG
// Using Inheritance concept.
class tech extends GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("Single Int Parameter");
  
        // Using super keyword to call
        // m1 method from class GFG.
        super.m1();
    }
}
  
// Main Class
public class Main {
    public static void main(String[] args)
    {
        // Creating object for tech class
        // obj is the object for tech class.
        tech obj = new tech();
  
        // tech class method m1
        // will be called here.
        // tech class extends GFG class
  
        // GFG class method m1 will be
        // called inside tech class method m1.
  
        // both method m1 will be called
        // one from tech class and
        // second from GFG class.
        obj.m1();
    }
}

Output :

Single Int Parameter
No parameter method

In the below example we have created an object of Programming which inherits two classes DP, CP. When the method learn is called using the object of Programming it goes to its own class Programming, in its class we have super.learn(); which calls the method super.learn() of its parent class and now after going to super.learn() in CP class it again calls super.learn() of its parent class i.e. from DP.

Java




// Java program to demonstrate the use
// of super keyword with Method
// Overloading
  
class DP {
    // Class DP method learn
    // with 0 parameter.
    public void learn() { System.out.println("Dynamic"); }
}
  
// Class CP extends class DP
class CP extends DP {
    
    // Class CP method learn
    // with 0 parameter.
    public void learn()
    {
        // Using super keyword
        // here learn will be
        // called from class DP.
        super.learn();
        System.out.println("Competetive");
    }
}
  
// Class programming extends class
// CP that is extends class DP.
class Programming extends CP {
    
    // Class programming method
    // learn with 0 parameter.
    public void learn()
    {
        // Here, learn method from class CP
        // and learn method from class DP
        // which called inside class CP learn
        // method.
        super.learn();
        System.out.println("Programming");
    }
}
  
// Main Class
public class A {
    public static void main(String[] args)
    {
        // Creating object for class Programming.
        Programming obj = new Programming();
        // here, learn method will be called
        // from Programming class.
  
        // method will be also called
        // from programming class extends
        // class CP that is extends
        // class DP.
        obj.learn();
    }
}

Output:

Dynamic
Competetive
Programming

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :