File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why and when to do method overloading and overiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why and when to do method overloading and overiding" Watch "why and when to do method overloading and overiding" New topic
Author

why and when to do method overloading and overiding

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
i did lot of google search, but all i found is:-
1.> How to do overriding and overloading,
2.> Definition that says,
Method overloading: is to change the behavior.
Method overriding: is to change the functionality.
3.> Rules to do both.
4.> Examples

what i wanted to know is:

Why to do both?
i mean may time we override toString(), but we add our own functionality into that, but the same same thing we can do in some other method also and than we can call that method, so why to use toString() only?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2172
    
  47
i mean may time we override toString(), but we add our own functionality into that, but the same same thing we can do in some other method also and than we can call that method, so why to use toString() only?

You can use you own method to print out a string representation of the object but the standard way to do it is to use toString() - all objects have a toString() method - and as many classes make use of this fact it's best to provide an implementation of it rather than creating your own method to do the same thing.

The difference between the two is with overriding you are changing the functionality of a method by providing your own implementation which has to have exactly the same method name and parameter list as the method you are overriding, with overloading you are implementing one or more methods with exactly the same name as the overloaded method but with different parameter lists either by the number of parameters and/or their type.
BTW I googled for "java override or overload" and found lots of articles explaining this.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2

You can use you own method to print out a string representation of the object but the standard way to do it is to use toString() - all objects have a toString() method - and as many classes make use of this fact it's best to provide an implementation of it rather than creating your own method to do the same thing.

standard way is the only reason of using method overloading and overriding.
toString() was just an example by the way.


The difference between the two is with overriding you are changing the functionality of a method by providing your own implementation which has to have exactly the same method name and parameter list as the method you are overriding, with overloading you are implementing one or more methods with exactly the same name as the overloaded method but with different parameter lists either by the number of parameters and/or their type.
BTW I googled for "java override or overload" and found lots of articles explaining this.


Yes these answers i also got by doing google, but this says, what is method overloading and overriding, and with this i am aware.
the thing which i wanted to know is why to use them?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2172
    
  47
standard way is the only reason of using method overloading and overriding.

Sorry, don't know what you mean by this.

the thing which i wanted to know is why to use them?

Overriding: Take the toString() example if you couldn't override any methods then every object would be stuck with the same implementation of toString() which doesn't return anything particularly useful. So you override it to return a String that reflects the contents of the object ie Date returns the date and time it is set to. ArrayList returns its contents which it gets by concatenating the results of calling the toString() method on all the objects it currently holds references to.

Overloading: Take the example of moving an item in a 2D world. You can move it by specifying how far to move in the x and y axis or by specifying an angle and distance. Both actions do the same ie move the item from A to B so you could use the same method name "move" to perform this task ie you could overload the method so that you have move(int x, int y) and move(double rad, int dist). You could of course use different method names but if you also allowed movement by specifying an Offset object and/or using a Point object etc etc your API would quickly get full of lots of different method names for essentially the same task and it would quickly become difficult for you to remember the correct name for the arguments you want to supply. Whereas if all the move type methods have the same name (ie move is overloaded) it is easy to use the API.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
ohh i got it.
Thanks for the explanation tony.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why and when to do method overloading and overiding
 
Similar Threads
method overwriting
overriding Method
Overloading & Overriding!
Question about polymorphism.
Overriding/overloading