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 i have no idea at all Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i have no idea at all " Watch "i have no idea at all " New topic
Author

i have no idea at all

joseph dela cruz
Ranch Hand

Joined: May 17, 2012
Posts: 38

i have been reading overloading methods and i have read how to do it and i tried it on my own. i just don't know if it is overloading already or if its a good practice in giving access through boolean or i could have just named the setMethod a different name. after i have read all the tutorials on the net and some of the old posts here i still don't know when and where to overload is it just a coding style?



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

if method name is same and parameter is different, then yes it is overloading.
i give different methods instead of just changing the parameters.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Kabayan Joseph,

First of all, no offense but the coding style (seemingly random capital letters in the names) in your sample code is atrocious. Please don't do that in real code. Secondly, what you show is not at all overloading. Overloading is when you use the same method name and return type but allow different parameters to be passed. A good example of overloading: see the many different types of parameters you can pass to String.valueOf()

Edit: See discussion regarding return type below


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

joseph dela cruz wrote:i still don't know when and where to overload is it just a coding style?

It definitely is not just a coding style. It is a common feature in many object-oriented languages and it helps reduce the conceptual overhead that might result if it were not available.


So, yes, it improves the code's readability and style but it's also a lot about conceptual weight (understandability).

---
(originally from Iligan City, in case you're wondering)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Junilu Lacar wrote: Secondly, what you show is not at all overloading. Overloading is when you use the same method name and return type but allow different parameters

return type can be different, the only thing is that parameter should differ. and the methods are overloaded...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

In case you're wondering how the examples I gave earlier makes a difference, here's how Pinoy and Kano might be implemented differently. Note that this brings the concepts of Overriding and Overloading together:


Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Seetharaman Venkatasamy wrote:return type can be different, the only thing is that parameter should differ. and the methods are overloaded...


Java Tutorials wrote:Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

If you want the return type to be different, define a different method. This forces you to use a different method name (not using overloading).
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
joseph dela cruz wrote:i still don't know when and where to overload

Overload when you require exactly the same behavior that you would get by calling a method that already exists, but you can't call it because the input you have is the wrong datatype.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Junilu Lacar wrote:If you want the return type to be different, define a different method. This forces you to use a different method name (not using overloading).

but if argument is different then you can have different return type, hence overloading.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Seetharaman Venkatasamy wrote:i give different methods instead of just changing the parameters.

In many cases, that could be a code smell and not particularly good object-orientation.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Junilu Lacar wrote:
Java Tutorials wrote:Overloaded methods are differentiated by the number and the type of the arguments passed into the method.

doesn't say anything about return type.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Dennis Deems wrote:Overload when you require exactly the same behavior that you would get by calling a method that already exists, but you can't call it because the input you have is the wrong datatype.

Overloaded methods behave differently internally. That is, there is different logic. String.valueOf(char) has different logic from String.valueOf(double). However, the end result in all cases of calling String.valueOf(whatever) is a String. There is an important distinction that you need to make between behavior and end result in this context. Also, regarding "wrong datatype" -- which of the overloaded versions of String.valueOf() would you say is dealing with the "wrong" datatype? It's not about right or wrong, it's about behaving/reacting differently to different types of input but getting back the same logical result. In my examples, the same logical result was a boolean: whether the person liked the dish or not.

I stand firmly by my assertion that using a different return type does not constitute proper use of overloading and if the compiler allowed it (do you guys have a working example?) -- then I will be truly disillusioned by Java.

Edits - qualified above statement with "proper use of"; also, not disillusioned with Java even though it allows different return types. I would seriously question the practice of taking advantage of that "feature" in real-world code though.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Seetharaman Venkatasamy wrote:Java Tutorials ... doesn't say anything about return type.

Exactly my point. Thank you for agreeing with me.

Edit: Ok, I concede that the compiler will allow the same method name and different parameters and apparently doesn't care if the return type is different. Maybe it's just me but I can't think of a reason to do that in real code. Why would you overload a concept that way? If something results in a different type, then it's a totally different class of behavior and should be given a different name. If the external manifestation (returning a String or Boolean or whatever) is the same but the stimulus (parameters passed) can be different, then there is a case for overloading. Just because the compiler allows it doesn't make it right, in my mind. But maybe that's just me and I'm past wanting to think about the language in nitty-gritty detail like this. Is there any example in the standard Java libraries that does overloading with different return types?

Can you see the problem with something like this?

If I were doing a code review on code like this, there would be some serious WTFs flying about and the author would likely be in the doghouse. To me that's not overloading. That's plain and simple conceptual abuse.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Junilu Lacar wrote:
Dennis Deems wrote:Overload when you require exactly the same behavior that you would get by calling a method that already exists, but you can't call it because the input you have is the wrong datatype.

Overloaded methods behave differently internally. That is, there is different logic. String.valueOf(char) has different logic from String.valueOf(double). However, the end result in all cases of calling String.valueOf(whatever) is a String. There is an important distinction that you need to make between behavior and end result in this context.

Well, I guess we have a different conception of what constitutes "behavior". In each case, valueOf returns the String representation of the given value. In my view, that is the same behavior. There are, of necessity, variations in how the behavior is accomplished. Different behavior, in my view, is exactly that which produces a different result.

Note to joseph: A common use of overloading is to supply a default value, as we see in String.toLowerCase and .toUpperCase. We can call these methods without passing a Locale, because the no-arg versions return to us the result of calling the method with Locale.getDefault() as the argument.

Also, regarding "wrong datatype" -- which of the overloaded versions of String.valueOf() would you say is dealing with the "wrong" datatype? It's not about right or wrong, it's about behaving/reacting differently to different types of input but getting back the same logical result.

Agreed; "wrong" is an infelicitous word choice. But you are writing from the point of view of a class whose design is completed and provides all the functionality we require, and if we are asking the question "when should I use overloading" then it might be helpful to consider the case of a class whose design we might want to modify in some way.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Junilu Lacar wrote: Is there any example in the standard Java libraries that does overloading with different return types?

from java.util.Arrays ;)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

Ok, Seetharaman, I'll give you a pass on that one. But I shake my head to think that this would even be considered a good practice in any other context. I think the whole point of overloading, or any other object-oriented mechanism, is to clarify the concepts involved. In the case you give, it makes sense for the return type to match the argument type. In the case I gave, that kind of "overloading" just muddies the idea of what "like()"-ing something means. I think this is the kind of context you have to deal with most of the time when you're writing real code so I tend to forget the corner cases like what you gave.

BTW, I'm thinking the examples you gave from the Java standard libs should probably be deprecated in favor of using Generics. Just off the top of my head, that is. There may be technical reasons to argue otherwise.

Cheers.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Junilu Lacar, your point is indeed valid in most of the cases .
Happy week end and smelling celebration here ....
Cheers
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

joseph dela cruz,
Your question about proper case usage for variable and class names was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i have no idea at all