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: method overloading, overriding
best ans
Excellent answer
Very perfect Answer What i was acpecting.
Thanks a lot.
superub ans…. thank u….
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
@satishassasin Glad to hear that
All your answers with example is so clear so that i will never forget the answers in my life
excellent answer
very good answer .i m very happy to see the answer .who’s person give ans r very intellegent
Very Nice Answer !!!!!!!!!!!!!!!!!!
bakwaassssssss!
its very easy to under stand
you’ve explained the difference very well……..
but may i have more differences…..
Thanks for uploading this code for us.
I find difficulty in overloading and overriding. Now i’m so clear. Thank u for do us the favour.
thanks a lot……..it’ a superb ans . before it i was very confuse b/w overloading and overriding ,but now its clear………….thanks again.
Clearly explained with suitable example
Thank youvery much good answer
Thanks! A nice and simple example is what I was looking for.
Tooooo bad answer
thanks .this site helped me a lot
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 ?
A very good answer and its was useful me to understand method overloading and method overriding.lot of thanx
i understood it very clearly bro….
Simple and efficiently done. Good job.
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
Thanks a lot! a brief description!
I was some confusing to under stand method overloading and method overriding but after reading above exam I got clear.
Thank you