What is the difference between method overloading and overriding?

224,038

Solution 1

Method overloading deals with the notion of having two or more methods in the same class with the same name but different arguments.

void foo(int a)
void foo(int a, float b)

Method overriding means having two methods with the same arguments, but different implementations. One of them would exist in the parent class, while another will be in the derived, or child class. The @Override annotation, while not required, can be helpful to enforce proper overriding of a method at compile time.

class Parent {
    void foo(double d) {
        // do something
    }
}

class Child extends Parent {

    @Override
    void foo(double d){
        // this method is overridden.  
    }
}

Solution 2

Method overriding is when a child class redefines the same method as a parent class, with the same parameters. For example, the standard Java class java.util.LinkedHashSet extends java.util.HashSet. The method add() is overridden in LinkedHashSet. If you have a variable that is of type HashSet, and you call its add() method, it will call the appropriate implementation of add(), based on whether it is a HashSet or a LinkedHashSet. This is called polymorphism.

Method overloading is defining several methods in the same class, that accept different numbers and types of parameters. In this case, the actual method called is decided at compile-time, based on the number and types of arguments. For instance, the method System.out.println() is overloaded, so that you can pass ints as well as Strings, and it will call a different version of the method.

Share:
224,038

Related videos on Youtube

Makoto
Author by

Makoto

Stack Overflow Inc. is treating the community which built it up as a dispensable tool in its quest to maintain relevance and market share. While I have no qualms with the network needing to make money, delivering a "product" and then asking the community to "add value" to it is not the path forward that I seek. In light of leadership's hard-line stance to this, the only form of civil disobedience we have at our disposal is to simply cease all moderation, which is the only real value-add we normal users of the site have brought.

Updated on July 09, 2022

Comments

  • Makoto
    Makoto almost 2 years

    What is the difference between overloading a method and overriding a method? Can anyone explain it with an example?

  • GriffeyDog
    GriffeyDog over 11 years
    @Override is not required. It's a good practice, but not required.
  • Hisham Muneer
    Hisham Muneer over 11 years
    Sorry @GriffeyDog. You are right "@Override" is not compulsory.But to remember that you are overriding a method, it is a good practice..Thanks for reminding..
  • happs
    happs over 10 years
    Overloading need not be in the same class but can be also be done between parent class and derived class.
  • barlop
    barlop about 10 years
    If you have a child class that defines the same method with different parameters then is that considered to be both overriding and overloading?
  • Vincent Zou
    Vincent Zou almost 9 years
    then it's overloading only as it's not overriding any of the parent method.
  • T.J. Crowder
    T.J. Crowder almost 9 years
    Re @happs's comment: It's still overloading if a parent class defines one signature, and a derived class defines a second signature, per JLS§8.4.9: "If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded."
  • Naim R.
    Naim R. about 2 years
    To add on that, the next question would be which method to execute? , the summary is :in overloading we follow the reference type in execution, in overriding we follow the object type in execution.