Can We Override Overloaded Method in Java?

Yes, we can override overloaded method in Java.

Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in superclass (or base class). The implementation to be executed is decided at run-time and decision is made according to the object used for call. Note that signatures of both methods must be same.

Overloading: Overloading is a feature of OOP languages like Java that is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters. Note that in Java, methods cannot be overloaded according to return type.

Here is an example code that demonstrates overriding of overloaded methods.

// Superclass 
public class Base { 
 
    public void display() { 
        System.out.println("No argument display method method from Base"); 
    } 
 
     // overloaded display method  
     public void display(String str)  { 
         System.out.println("One argument display method method from Base " + str); 
    } 
} 
 
//Subclass
public class Derived extends Base { 
 
    // Overriding the no-arg display  
    public void display() { 
         System.out.println("No argument display method method from Derived"); 
    } 
 
    // Overriding the one-arg display
    public void display(String str)  { 
        System.out.println("One argument display method method from Derived " + str); 
   } 
} 
 
public class Test { 
    public static void main(String args[ ])  { 
       Base obj1 = new Base(); 
       obj1.display();
       obj1.display("Hello");
       obj1 = new Derived();
       obj1.display();
       obj1.display("Hello");
    } 
} 
 
 
Output:
No argument display method method from Base
One argument display method method from Base Hello
No argument display method method from Derived
One argument display method method from Derived Hello

Hope you have enjoyed reading this post. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.