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

56 Responses to “Method overriding vs overloading”

  1. anshuman says:

    MADARCHOD

  2. dp says:

    all non private methods in Java are polymorphic because which method is invoked is decided at runtime based on the object instead of compile time -> overloaded methods are not a compile time phenomenon.

    simple example: Class A defines method1() and method2(int). Class B extends A and overrides both methods.

    B b = new B();
    b.method2(5);

    when it will be decided which method2 is called on object b?

  3. YELLOW says:

    Thanks for the clear tutorial!

  4. Transparent Glazz says:

    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)

  5. Narayan says:

    100% error

  6. priyatham says:

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

  7. aa says:

    after admin edit the article tho

  8. Nandeesh says:

    can you explain how method overriding is run time phenomenon?

  9. harry says:

    Thanks bro

  10. Wajid Ali says:

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

  11. Wajid Ali says:

    wow .. really helpful information.. nice …

  12. TyrantInShorts says:

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

  13. Aravind M says:

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

  14. c says:

    valid as parameter type is changed

  15. mRzErO says:

    Great explanation, Thank you.

  16. @prashant Gupta says:

    Simple and very clear Explanation thank you :)

  17. Narendra says:

    Its very good explenation

  18. aot2002 says:

    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.

  19. MichaelM says:

    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.

  20. Aleksandro says:

    It is pretty clear! Thanks

  21. saifur rahman says:

    valid

  22. Dharmendra Singh says:

    //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..?

  23. prajakta says:

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

  24. Veera says:

    Explained nicely… thx…

  25. VIJAYKUMAR says:

    Good examples nice explanation..

  26. Tr4sH says:

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

  27. Balaji BaajiPuthiran says:

    It’s Very Useful……Thankx

  28. Shivi Kashyap says:

    Equals() from Object class has vital role here.

  29. krishna says:

    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

  30. Jaswant Singh Sodha says:

    Really helpful

  31. Thanuja says:

    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.

  32. RAJ says:

    Great Explanation, with example…..

  33. Joseph Smith says:

    this helps.

  34. vanloi says:

    thanks you.

  35. sid says:

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

  36. cjiang says:

    “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.

  37. Niks says:

    Wonderful article

  38. Jenny says:

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

  39. anonymous says:

    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!

  40. prateek says:

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

  41. overloading says:

    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!

  42. Mitul says:

    overloading is static polymorphism whereas overriding is dynamic polymorphism

  43. varoon says:

    THanks! I added that point to the article.

  44. ankit says:

    really helpful

  45. Karan Gupta says:

    nice explanation (y)

  46. sanju says:

    really precise to understand the concept…..

  47. faraz007 says:

    very clearly understood..thanx

  48. Srini says:

    Not good

  49. yash says:

    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.. ?

  50. anonyms says:

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

  51. kavya says:

    really its cooolllll……….thank q

  52. Drishti says:

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

  53. Abhishek says:

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

  54. pankaj says:

    thx…………

  55. Milan says:

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

Leave a Reply

*