Overriding and Overloading Help Needed

I am reading Head First Java and i was not able to understand the concept of method overriding (something to do with arguments) and overloading...

i understood inheritance and that sub class can override a superclass but i didn't get how method overriding works and what is overriding to do with arguments and return types.

Can anyone please explain me what is method overriding and overloading??? And if you can even explain what are arguments and return types.... that would be great...

thanks....

dileep keely

Ranch Hand

Posts: 112

posted 7 years ago

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:

Campbell Ritchie

Marshal

Posts: 56546

172

posted 7 years ago

What the previous post means is, run those classes with static (not [b] [/b]) before "methd", then delete the word static from both classes before "methd" and see the difference.

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.

Overriding.

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