It is the ability of the object to behave differently for the same message.
Overriding gives the benifit of having the behaviour specific to the subclass.->Which method to be called is known at compile time.Addng on the access level can be less restrictive then the overridden method.And can throw runtime exception even if the overridden method doesn't.
Overloading cleanliness of code .Take an example of valueOf(Object obj).It takes boolean,char,int ..-->Which method to be called is know at run time.
In the below example try removing the static before methd and try it gives the overriding behavior:
For overloading try the below:I have changed the arguments in the method:
Overriding and Overloading are two different things, its easy to get them confused
Overloading, in my opinion is the easier one to understand
Overloading basically means having more than one method that uses the same name
For example, you may have the following method in your code :
Thats all fine and dandy, but what if you want to call the same method, but you don't want to pass a String in? Maybe you have an int instead, you can do this :
Making sense? OK, put those two methods into your IDE, such as eclipse. Then, underneath those, hold control and press space, this should bring up a window of available members. You should see both those methods there.
You can call either method depending on what you want to do.
The method names must be the same, and the input values (arguements) must be different types. Read that chapter in head first again, they have some good examples
Also, take a look at the String API, you can see that several methods have the same name, but different arguements. Have a look at what they do, this will help you understand reasons for doing overloading.
Say you have a class called Car, it has a method called driveFast().
Then you create a class called Mustang, that extends Car.
Because it extends car, it (not worrying about access levels), will have access to driveFast()
This might be ok for you, but maybe you don't want to use driveFast() on Car, but you want to provide your OWN implementation, that is specific to Mustang, that is what overriding is good for