BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference between method Overloading and Overriding in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will discuss the difference between overloading and overriding in Java. If you are new to these terms then refer the following posts:

  1. Method overloading in java
  2. Method overriding in java

Overloading vs Overriding in Java

  1. Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.
  2. Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.
  3. The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.
  4. Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
  5. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
  6. private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.
  7. Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).
  8. Argument list should be different while doing method overloading. Argument list should be same in method Overriding.

Overloading example

//A class for adding upto 5 numbers
class Sum
{
    int add(int n1, int n2) 
    {
        return n1+n2;
    }
    int add(int n1, int n2, int n3) 
    {
        return n1+n2+n3;
    }
    int add(int n1, int n2, int n3, int n4) 
    {
        return n1+n2+n3+n4;
    }
    int add(int n1, int n2, int n3, int n4, int n5) 
    {
        return n1+n2+n3+n4+n5;
    }
    public static void main(String args[])
    {
    	Sum obj = new Sum();
    	System.out.println("Sum of two numbers: "+obj.add(20, 21));
    	System.out.println("Sum of three numbers: "+obj.add(20, 21, 22));
    	System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23));
    	System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24));
    }
}

Output:

Sum of two numbers: 41
Sum of three numbers: 63
Sum of four numbers: 86
Sum of five numbers: 110

Here we have 4 versions of same method add. We are overloading the method add() here.

Overriding example

package beginnersbook.com;
class CarClass
{
    public int speedLimit() 
    {
        return 100;
    }
}
class Ford extends CarClass
{
    public int speedLimit()
    {
        return 150;
    }
    public static void main(String args[])
    {
    	CarClass obj = new Ford();
    	int num= obj.speedLimit();
    	System.out.println("Speed Limit is: "+num);
    }
}

Output:

Speed Limit is: 150

Here speedLimit() method of class Ford is overriding the speedLimit() method of class CarClass.

Top Related Articles:

  1. User defined exception in java
  2. How to write to file in Java using BufferedWriter
  3. Method Overloading “Reference is Ambiguous” error in Java
  4. Constructor Overloading in Java with examples
  5. Method Overloading with Autoboxing and Widening in Java

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. siva devara says

    September 27, 2014 at 8:05 PM

    Thank you sir

    Reply
    • Sujitha says

      November 15, 2014 at 5:50 AM

      Thank you!!!!!!!!Easy to Understand

      Reply
  2. Mariyappa says

    November 7, 2014 at 8:11 AM

    Is 7th point is correct?

    Reply
    • Chaitanya Singh says

      November 28, 2014 at 4:47 AM

      Yes It is.

      Reply
  3. jemima says

    November 26, 2014 at 3:45 PM

    hi sir,
    I’m not sure whether I’m correct i studied both method overloading and overridding then I’m studying there difference..i think the 7th point is incorrect.. it states “Return type of overloaded method should be same as the other methods of the same name however return type of overriding method can be different from overridden method.” and i think it must be ” the return type of overloading methods can differ but the return type of overriding methods should be the same..”… if im wrong plz help me to identify my mistake .
    thank u so much

    Reply
    • Chaitanya Singh says

      November 28, 2014 at 4:43 AM

      Hello Jemima,

      Point#7 is correct, overridden methods can have different return types. You can refer http://stackoverflow.com/questions/14694852/can-overridden-methods-differ-in-return-type.

      Reply
  4. Kelele says

    November 30, 2014 at 8:04 AM

    i think overridden must have the return type

    Reply
  5. RAJ says

    December 17, 2014 at 12:31 PM

    For the 7th point `Return type of overloaded methods should be same`. Is it always necessary for methods to be overloaded?
    What about this example?

    public String getData(){
    return “Employe from Manager”;
    }
    public int getData(String val){
    return 0;
    }

    Reply
  6. vishal says

    December 30, 2014 at 12:09 PM

    what is the use of super keyword while we are overiding a function can you please explain..

    Reply
    • Ramesh Kumar says

      January 5, 2016 at 5:05 PM

      super keyword calls the parent class method

      Reply
  7. Yogesh says

    January 14, 2015 at 8:19 AM

    I like ur explanation ……

    Reply
  8. pragati says

    March 4, 2015 at 1:31 PM

    thanks sir

    Reply
  9. Arpitha says

    April 9, 2015 at 10:26 AM

    Can u please explain me 7th point ? I think it is wrong right? return type doesn’t matter in case of overloading and return type should be same for method overriding.

    Reply
  10. Abiola says

    October 29, 2015 at 2:09 PM

    nice way of teaching cant believe it. am learning java just like that.

    Reply
  11. Akshay pattar says

    June 9, 2016 at 5:20 PM

    CarClass obj = new Ford();

    plz answer
    y cant we write it as
    carclass obj = new car ();

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap