This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Overriding and Overloading Help Needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding and Overloading Help Needed" Watch "Overriding and Overloading Help Needed" New topic
Author

Overriding and Overloading Help Needed

Arjun Bajaj
Greenhorn

Joined: Apr 21, 2010
Posts: 13
Hey Guys,

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

Joined: Jun 28, 2010
Posts: 91
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
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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.

We have an FAQ which may be helpful to you.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

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


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding and Overloading Help Needed