In Java, what’s the difference between method overloading and method overriding?

PD9waHAgaW5jbHVkZSAoJ2Fkc2Vuc2VfdGV4dEltYWdlLnBocCcpOyA/Pg==

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly easy to understand with the examples we present below. Let’s start the discussion by talking more about method overloading first. Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method). Now, two or more methods with the same name in the same class sounds simple enough to understand. But, what do we mean exactly by different parameters? Well, let’s consider a very simple example.

Suppose we have a class called TestClass which has two methods, and both methods have the same name. Let’s say that name is “someMethod”. Those two methods would be considered to be “overloaded” if if one or both of these conditions is true:

The conditions for method overloading

1.) The number of parameters is different for the methods.
2.) The parameter types are different (like 
changing a parameter that was a float to an int).  

How to NOT overload methods:

It’s also very important to understand that method overloading is NOT something that can be accomplished with either, or both, of these two things:

1.  Just changing the return type of the method.  
If the return type of the method is the only thing 
changed, then this will result in a compiler error.  

2.  Changing just the name of the method parameters, but 
not changing the parameter types.  If the name of the 
method parameter is the only thing changed then this 
will also result in a compiler error.    

Confused? Well, here are some very helpful examples of where overloading would be both valid and invalid – pay attention to the comments as well:

Examples of Method Overloading in Java – both valid and invalid:

//compiler error - can't overload based on the   
//type returned -
//(one method returns int, the other returns a float):    

int changeDate(int Year) ;  
float changeDate (int Year);    

//compiler error - can't overload by changing just 
//the name of the parameter (from Year to Month):    

int changeDate(int Year);   
int changeDate(int Month) ;  
 
//valid case of overloading, since the methods
//have different number of parameters:        

int changeDate(int Year, int Month) ;  
int changeDate(int Year);    

//also a valid case of overloading, since the   
//parameters are of different types:    

int changeDate(float Year) ;  
int changeDate(int Year);  

Overloading happens at compile time

Another important point to remember is that overloading is a compile time phenomenon. This just means that the compiler determines whether a given method(s) is correctly overloaded, and if not a compiler error is returned as shown in the examples above.

Subscribe to our newsletter for more free interview questions.

What about method overriding?

PD9waHAgaW5jbHVkZSAoJ2Fkc2Vuc2UxLnBocCcpOyA/Pg==

Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

Example of method overriding

Let’s go through a simple example to illustrate what method overriding would look like:

public class Parent {

 public int someMethod() {
   
   return 3;
       
    }
}


public class Child extends Parent{

 // this is method overriding:
 public int someMethod() {

    return 4;
       
    }

}

In the sample code above, someMethod is an overridden method in the Child class, because it has the exact same name, number of parameters, and return type as the someMethod method defined inside it’s parent class (conveniently named Parent).

Overriding happens at run time

Another important point to remember is that overriding is a run time phenomenon – not a compile time phenomenon like method overloading.

PD9waHAgaW5jbHVkZSAoJ2Fkc2Vuc2UtbWVkLXJlY3QyMDE0LnBocCcpOyA/Pg==

Summary of differences between overloading and overriding

Let’s summarize the differences between overloading and overriding. When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. Overriding means that a method inherited from a parent class will be changed. But, when overriding a method everything remains exactly the same except the method definition – basically what the method does is changed slightly to fit in with the needs of the child class. But, the method name, the number and types of parameters, and the return type will all remain the same.

And, method overriding is a run-time phenomenon that is the driving force behind polymorphism. However, method overloading is a compile-time phenomenon.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • YELLOW

    Thanks for the clear tutorial!

  • Transparent Glazz

    Here is one more valid example of Overloading wherein at least order/sequence of parameters types to be passed is different

    e.g. returntype1 methodName_A(int s, float r)
    returntype1 methodName_A(float r, int s)

  • priyatham

    int changeDate(int Year);
    float changeDate(int Month) ;
    does this give compile time error

    • Narayan

      100% error

  • Nandeesh

    can you explain how method overriding is run time phenomenon?

  • harry

    Thanks bro

  • https://www.facebook.com/wajid.ali.52?ref=tn_tnmn Wajid Ali

    wow .. really helpful information.. nice …

  • TyrantInShorts

    You should probably also add something about argument type covariance and return type covariance with respect to overloading and overriding. Very good article though.

  • Aravind M

    really it was some what helpful…nd need to update some more information….Thank Q

  • mRzErO

    Great explanation, Thank you.

  • @prashant Gupta

    Simple and very clear Explanation thank you :)

  • Narendra

    Its very good explenation

  • aot2002

    It might be easier to state that overriding could be used to manipulate the data in the method differently then the parent class. Example you might want to add a string to each item in a list or you might want to remove items from a list before returning. Where as the parent class method doesn’t currently do this.

  • MichaelM

    How many have used both but prior to this would have had a hard time articulating it? The point being how good would it really be to ask for a job interview? Probably if you read this first.

  • Aleksandro

    It is pretty clear! Thanks

  • Dharmendra Singh

    //here return type of method is different with same method name different //parameter type

    int foo(int b);
    float foo(float b);
    My question is here, is it valid case of overloading or invalid case of overloading..?

    • saifur rahman

      valid

    • c

      valid as parameter type is changed

  • prajakta

    thanx alot. it helped me to sort my forever confusion from ever since i have come to know about these topics.

  • Veera

    Explained nicely… thx…

  • VIJAYKUMAR

    Good examples nice explanation..

  • Tr4sH

    Man, this explanation is what i was finding and now I understand that.

  • Balaji BaajiPuthiran

    It’s Very Useful……Thankx

  • Shivi Kashyap

    Equals() from Object class has vital role here.

  • Jaswant Singh Sodha

    Really helpful

  • Thanuja

    I am sure no one can score 100% in following quiz on Java Overloading
    http://www.swquiz.com/java/core-java/overloading

    I could not understand why

    TitBitOverloading2 & TitBitOverloading3
    have different answers.

  • RAJ

    Great Explanation, with example…..

  • Joseph Smith

    this helps.

  • vanloi

    thanks you.

  • sid

    Very easy to understand. overriding example made it easy of all.Awesome.. :)

  • cjiang

    “An overridden method would have the exact same method name, return
    type, number of parameters, and types of parameters as the method in the
    parent class, and the only difference would be the definition of the
    method.”
    Different return type is allowed, at a cert level. To be exact, a subclass return type of the original return type is ok.

  • Niks

    Wonderful article

  • Jenny

    The information provided above was extremely helpful to me. Very
    well explained. Keep up the good work. Thank you.

  • anonymous

    could also mention adding @Override above the overriding method to key to the compiler that the method is intended to override another. Helps watch out for errors in typing and all that, very good job though!

  • overloading

    If you have two methods:

    public void someMethod()

    and

    public void someMethod(int aNumber)

    would this count as overloading? If so, why?

    Thanks in advance!

    • prateek

      because there is a difference in parameter….and method name is same

    • krishna

      Yes that is overloading, When you call SomeMethod() it will execute SomeMethod() function and if you call someMethod(10) the it will execute SomeMethod(int aNumber) function

  • Mitul

    overloading is static polymorphism whereas overriding is dynamic polymorphism

  • ankit

    really helpful

  • Karan Gupta

    nice explanation (y)

  • sanju

    really precise to understand the concept…..

  • faraz007

    very clearly understood..thanx

  • preet
  • Srini

    Not good

  • yash

    totally wrong… method override cant change the number of parameter in child class.. and wht about if perent class method throws any exeption can we override it or not,, if yes then we need to throw what kind of exception.. ?

  • anonyms

    You forgot to mention one point.
    Overriding is a runtime phenomenon whereas overloading is a compile time phenomenon.

    • varoon

      THanks! I added that point to the article.

    • https://www.facebook.com/wajid.ali.52?ref=tn_tnmn Wajid Ali

      hi plz check Admin has mention above.. read carefully thanks

      • aa

        after admin edit the article tho

  • kavya

    really its cooolllll……….thank q

  • Drishti

    Really good job! Awesome compiled blog. Would love it if you have some questions too!

  • Abhishek

    I think you should also touch upon the Exception thrown by the method and their role in Overloading/Overriding

  • pankaj

    thx…………

  • Milan

    The above information is really helpful and whatever has been explained is really interesting and easy to understand.