Open In App

Difference Between Method Overloading and Method Overriding in Java

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The differences between Method Overloading and Method Overriding in Java are as follows:

Method Overloading

Method Overriding

Method overloading is a compile-time polymorphism.Method overriding is a run-time polymorphism.
Method overloading helps to increase the readability of the program.Method overriding is used to grant the specific implementation of the method which is already provided by its parent class or superclass.
It occurs within the class.It is performed in two classes with inheritance relationships.
Method overloading may or may not require inheritance.Method overriding always needs inheritance.
In method overloading, methods must have the same name and different signatures.In method overriding, methods must have the same name and same signature.
In method overloading, the return type can or can not be the same, but we just have to change the parameter.In method overriding, the return type must be the same or co-variant.
Static binding is being used for overloaded methods.Dynamic binding is being used for overriding methods.
Poor Performance due to compile time polymorphism.It gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.
Private and final methods can be overloaded.Private and final methods can’t be overridden.
The argument list should be different while doing method overloading.The argument list should be the same in method overriding.

Method Overloading in Java

Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading, the return type can or can not be the same, but we have to change the parameter because, in java, we can not achieve method overloading by changing only the return type of the method. 

Example of Method Overloading: 

Java
// Java Program to Implement
// Method Overloading
import java.io.*;

class MethodOverloadingEx {

    static int add(int a, int b) { return a + b; }

    static int add(int a, int b, int c)
    {
        return a + b + c;
    }

    // Main Function
    public static void main(String args[])
    {
        System.out.println("add() with 2 parameters");
        // Calling function with 2 parameters
        System.out.println(add(4, 6));

        System.out.println("add() with 3 parameters");
        // Calling function with 3 Parameters
        System.out.println(add(4, 6, 7));
    }
}

Output
add() with 2 parameters
10
add() with 3 parameters
17

Method Overriding in Java

Method Overriding is a Run time polymorphism. In method overriding, the derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, the return type must be the same or co-variant (return type may vary in the same direction as the derived class). 

Example of Method Overriding:

Java
// Java Program to implement
// Method Overriding
import java.io.*;

// Base Class
class Animal {
    void eat()
    {
        System.out.println("eat() method of base class");
        System.out.println("eating.");
    }
}

// Inherited Class
class Dog extends Animal {
    void eat()
    {
        System.out.println("eat() method of derived class");
        System.out.println("Dog is eating.");
    }
}

// Driver Class
class MethodOverridingEx {
    // Main Function
    public static void main(String args[])
    {
        Dog d1 = new Dog();
        Animal a1 = new Animal();

        d1.eat();      
        a1.eat();

          // Polymorphism: Animal reference pointing to Dog object
        Animal animal = new Dog();
      
          // Calls the eat() method of Dog class
        animal.eat(); 
    }
}

Output
eat() method of derived class
Dog is eating.
eat() method of base class
eating.
eat() method of derived class
Dog is eating.

Explanation of the above Program:

Here, we can see that a method eat() has overridden in the derived class name Dog that is already provided by the base class name Animal. When we create the instance of class Dog and call the eat() method, we see that only derived class eat() method run instead of base class method eat(), and When we create the instance of class Animal and call the eat() method, we see that only base class eat() method run instead of derived class method eat(). 



Previous Article
Next Article

Similar Reads

Difference between Method Overloading and Method Overriding in Python
Method Overloading: Method Overloading is an example of Compile time polymorphism. In this, more than one method of the same class shares the same method name having different signatures. Method overloading is used to add more to the behavior of methods and there is no need of more than one class for method overloading.Note: Python does not support
3 min read
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code
3 min read
Exception Handling with Method Overriding in Java
An Exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run-time, that disrupts the normal flow of the program’s instructions. Exception handling is used to handle runtime errors. It helps to maintain the normal flow of the program. In any object-oriented programming language, Overriding is a feature t
6 min read
Different Ways to Prevent Method Overriding in Java
Inheritance is a substantial rule of any Object-Oriented Programming (OOP) language but still, there are ways to prevent method overriding in child classes which are as follows: Methods: Using a static methodUsing private access modifierUsing default access modifierUsing the final keyword method Method 1: Using a static method This is the first way
5 min read
Java Program to Use Method Overriding in Inheritance for Subclasses
Method overriding in Java is when a subclass implements a method that is already present inside the superclass. With the help of method overriding we can achieve runtime polymorphism. When we are overriding a method then we must keep three things in mind. The method in the subclass must have the same name as the method present inside the superclass
5 min read
Java - Covariant Method Overriding with Examples
The covariant method overriding approach, implemented in Java 5, helps to remove the client-side typecasting by enabling you to return a subtype of the overridden method's actual return type. Covariant Method overriding means that when overriding a method in the child class, the return type may vary. Before java 5 it was not allowed to override any
4 min read
When We Need to Prevent Method Overriding in Java ?
Here we will be discussing why should we prevent method overriding in java. So, before going into the topic, let's give a look at the below important concept then we will move to the actual topic. As we know Object Oriented Programming (OOPs) concept consists of 4 important concepts that are as follows: EncapsulationInheritancePolymorphismAbstracti
4 min read
Overriding equals method in Java
Consider the following Java program: Java Code class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } } // Driver class to test the Complex class public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); Complex c2 = new Complex(10, 15); if (c1 == c2) { System
3 min read
Overriding toString() Method in Java
Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they were not using the same but getting string representa
3 min read
Variables in Java Do Not Follow Polymorphism and Overriding
Variables in Java do not follow polymorphism. Overriding is only applicable to methods but not to variables. In Java, if the child and parent class both have a variable with the same name, Child class's variable hides the parent class's variable, even if their types are different. This concept is known as Variable Hiding. In the case of method over
2 min read