The Wayback Machine - https://web.archive.org/web/20240829143200/https://www.geeksforgeeks.org/difference-between-method-overloading-and-method-overriding-in-java/
Open In App

Difference Between Method Overloading and Method Overriding in Java

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.
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 type of runtime polymorphism. In method overriding, a method in a derived class has the same name, return type, and parameters as a method in its parent class. The derived class provides a specific implementation for the method that is already defined in the parent class.

Example of Method Overriding:

Java
import java.io.*;

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

// Derived Class
class Dog extends Animal {
    @Override
    void eat() {
        System.out.println("eat() method of derived class");
        System.out.println("Dog is eating.");
    }
    
    // Method to call the base class method
    void eatAsAnimal() {
        super.eat();
    }
}

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

        // Calls the eat() method of Dog class
        d1.eat();
        // Calls the eat() method of Animal class
        a1.eat();

        // Polymorphism: Animal reference pointing to Dog object
        Animal animal = new Dog();
        
        // Calls the eat() method of Dog class
        animal.eat();
        
        // To call the base class method, you need to use a Dog reference
        ((Dog) animal).eatAsAnimal();
    }
}

Output
eat() method of derived class
Dog is eating.
eat() method of base class
Animal is eating.
eat() method of derived class
Dog is eating.
eat() method of base class
Animal 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
Difference Between Function Overloading and Function Overriding in JavaScript
Function overloading and function overriding are two important concepts in object-oriented programming (OOP). Function overloading allows the creation of multiple functions with the same name but different parameters. Function overriding allows the subclass or child class to provide the specific implementation of the method that is already defined
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 - 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
Overriding in Java
In Java, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then th
13 min read
Output of Java program | Set 18 (Overriding)
Prerequisite - Overriding in Java 1) What is the output of the following program? class Derived { protected final void getDetails() { System.out.println("Derived class"); } } public class Test extends Derived { protected final void getDetails() { System.out.println("Test class"); } public static void main(String[] args) { Derive
3 min read
Overriding methods from different packages in Java
Prerequisite : Overriding in Java, Packages in Java Packages provide more layer of encapsulation for classes. Thus, visibility of a method in different packages is different from that in the same package. How JVM find which method to call? When we run a java program, JVM checks the runtime class of the object. JVM checks whether the object's runtim
3 min read
Method Overriding with Access Modifier
Prerequisites: Method Overriding in java and Access Modifier in Java Method Overriding In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by its super-class or parent class. When a method in a subclass has the same name,
3 min read
Overriding of Thread class start() method
Whenever we override start() method then our start() method will be executed just like a normal method call and new thread wont be created. We can override start/run method of Thread class because it is not final. But it is not recommended to override start() method, otherwise it ruins multi-threading concept. // Java program illustrate the concept
2 min read
Difference between Multi-methods and Overloading
In multi-methods we use the collection of methods. And of all methods have the same name, the same number of arguments, and can have overlapping type signatures. In the case of multi-methods when a function call is made then a collection of all the methods is considered as possible methods. Depending upon the argument at run-time, the correct metho
4 min read
Method overloading and null error in Java
In Java it is very common to overload methods. Below is an interesting Java program. Java Code public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) "); } // Driver code public static void main
2 min read
Method Overloading and Ambiguity in Varargs in Java
Prerequisite - Varargs , Method Overloading Method Overloading in Varargs Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-length argument by following ways: Case 1 - Methods wi
5 min read
Method Overloading with Autoboxing and Widening in Java
Let us go through the basic pre-requisites such as Method Overloading, Autoboxing, and Unboxing. So method overloading in java is based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler
7 min read
Method Overloading in Java
In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both. Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding. In Method overloading c
9 min read
Overloading Variable Arity Method in Java
Here we will be discussing the varargs / variable arity method and how we can overload this type of method. So let us first understand what a variable arity method is and its syntax. A variable arity method also called as varargs method, can take a number of variables of the specified type. Note: Until version 1.4 there is no varargs method. It was
5 min read
super keyword for Method Overloading in Java
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
4 min read
Invalid Method Overloading in Java
Overloaded methods are those which belong to the same class, having the same name but different arguments. The concept of method overloading came from polymorphism. Literally "poly" means many and "morphism' means form. Consider a real-life example of water as polymorphic because it can take many forms (Solid, Liquid, and gas). Similarly, in java,
4 min read
Method Overloading in Different Classes in Java
Methods of a class said to be overloaded if Both declared in the same class, orBoth inherited by a class, orOne declared and one inherited And have the same name but a different signature. Refer to this article, Different ways of Method Overloading in Java, for a basic understanding of method overloading. The below programs demonstrate the same. Ca
3 min read
Different ways of Method Overloading in Java
If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.If we have to perform only one operation, having same name of the methods increases the readability of the program.Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you
7 min read
Constructor Overloading in Java
Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. For example, the
5 min read
Constructor Overloading with Static Block in Java
In Java, Constructor is a block of codes similar to the method that is used to initialize the object’s state. A constructor is invoked at the time of object or instance creation. Each time an object is created using a new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of
4 min read
Automatic Type Promotion in Overloading in Java
Before going into the actual topic, first, we need to know about method overloading and type promotions. What is Method Overloading?When a class consists of more than one method with the same name but with different signatures and return types, then we call those overloaded methods, and the process is called method overloading. Example: void method
6 min read
GFact 48 | Overloading main() in Java
Consider the below Java program. // A Java program with overloaded main() import java.io.*; public class Test { // Normal main() public static void main(String[] args) { System.out.println("Hi Geek (from main)"); Test.main("Geek"); } // Overloaded main methods public static void main(String arg1) { System.out.println("Hi,
2 min read
Output of Java program | Set 22 (Overloading)
Prerequisite - Overloading in Java1) What is the output of the following program? Java Code public class Test { public int getData() //getdata() 1 { return 0; } public long getData() //getdata 2 { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData()); } } a) 1 b) 0 c) Runtime error d) Compilat
3 min read