Method overriding and overloading in Java

I know this may be trivial for many people but then it can be pretty confusing for new bees (pssst.. in the beginning me too used to be confused between overloading and overriding)

OVERRIDING – when you extend a class and write a method in the derived class which is exactly similar to the one present in the base class, it is termed as overriding.

Example:

public class BaseClass{

public void methodToOverride()

{

//Some code here

}

}

public class DerivedClass extends BaseClass{

public void methodToOverride()

{

//Some new code here

}

}

As you can see, in the class DerivedClass, we have overridden the method present in the BaseClass with a completely new piece of code in the DerivedClass.

What that effectively means is that if you create an object of DerivedClass and call the methodToOverride() method, the code in the derivedClass will be executed. If you hadn’t overridden the method in the DerivedClass then the method in the BaseClass would have been called.

OVERLOADING -  when you have more than one method with the same name but different arguments, the methods are said to be overloaded.

Example:

public class OverLoadingExample{

public void add(int i, int j)

{

int k = i + j;

}

public void add(String s, String t)

{

int k = Integer.parseInt(s) + Integer.parseInt(t);

}

}

As you can see in the example above, we have the same method add() taking two parameters but with different data types. Due to overloading we can now call the add method by either passing it a String or int :)

Tags: ,

28 comments

  1. Excellent answer

  2. Very perfect Answer What i was acpecting.
    Thanks a lot.

  3. superub ans…. thank u….

  4. thankyou very much…the best part is that u have explained the concept with a example. i can remember that for age’s :) thanks brother :) best answer ever. i will share this :)

  5. @satishassasin Glad to hear that :)

  6. All your answers with example is so clear so that i will never forget the answers in my life

  7. very good answer .i m very happy to see the answer .who’s person give ans r very intellegent

  8. Very Nice Answer !!!!!!!!!!!!!!!!!!

  9. its very easy to under stand

  10. you’ve explained the difference very well……..

    but may i have more differences…..

  11. Thanks for uploading this code for us.

  12. Praveen Kumar

    I find difficulty in overloading and overriding. Now i’m so clear. Thank u for do us the favour.

  13. thanks a lot……..it’ a superb ans . before it i was very confuse b/w overloading and overriding ,but now its clear………….thanks again.

  14. Clearly explained with suitable example

  15. Thank youvery much good answer

  16. Thanks! A nice and simple example is what I was looking for.

  17. Tooooo bad answer

  18. thanks .this site helped me a lot

  19. The example was good but my question is , suppose we have Class A with a method name add() and a Class B extends A with method add(int a). Is it overloading or overriding ?

  20. A very good answer and its was useful me to understand method overloading and method overriding.lot of thanx

  21. i understood it very clearly bro….

  22. Simple and efficiently done. Good job.

  23. you have tried good and so the basic is being given by you,
    still its a concept but we need more clarifications,use and some more practical examples….thanks a lot for your best and answering in a simplest way….

    VED PRAKASH MISHRA
    Infinite comp. sol,B’lore

  24. Thanks a lot! a brief description!

  25. I was some confusing to under stand method overloading and method overriding but after reading above exam I got clear.

    Thank you

Leave a comment