views:

317

answers:

7

Overriding a method can be prevented by using the keyword final, likewise how to prevent overloading?

+11  A: 

You can't do that in Java.

An overloaded method is basically just another method.

An attempt could look (something) like this

void yourMethod(String arg) { /* ... */ }

void yourMethod(String arg, Object... prevent) {
    throw new IllegalArgumentException();
}

but it won't work since Java resolves overloading by looking at the "best match" or "most specific signature".

The method could still be overloaded with a

void yourMethod(String arg, String arg2) { /* ... */ }

which would be called when doing yourMethod("hello", "world").

Btw, why would you want to prevent overloading? Perhaps there is another way of doing what you want?

aioobe
+2  A: 

Mark the class final and it can't be extended. That may defeat some other purpose though.

Lauri Lehtinen
...such as overriding :-)
aioobe
A: 

If you overload a method, you've created a method with the same name but different parameter types. In Java, a method is defined (partly) in terms of its parameters, and thus if two methods have the same name but different parameters, then they are apples and oranges, i.e., not the same. In short, you can't prevent someone from creating new methods in a class, which is essentially what you're asking for.

Shakedown
A: 

Overriding and Overloading are different techniques. Overloading is a way of declaring multiple methods with the same names but different parameter types or different no of parameters.

Sujee
Overloading is not an OOP technique at all. It's a handy property that uses static compile-time information to give two possibly completely unrelated functions the same name in source code.
Lajla
Thank you Lajla. I didn't know that it is not part of OOP.
Sujee
Removed word 'OOP' from my answer as I misunderstood that overloading is one of the OOP technique.
Sujee
A: 

You can prevent a method from being overwritten by making it final, but you cannot prevent a method from being overloaded. Well technically you could make the class itself final, so that no methods at all can be added by creating a subclass but I don't think that's a good design.

perdian
Methods are overridden, not overwritten.
Michael Borgwardt
+13  A: 

You can't. Because it's almost pointless.

If you want to overload the method handle(..) but you are disallowed to, you'd create doHandle(..) instead.

Which overloaded method is used is determined at compile time (in contrast to overridden methods, which are determined at runtime). So the point of overloading is sharing a common name for common operations. Disallowing that is a matter of code-style, rather than anything else.

Bozho
+1 for pointless. A compiler will never ever pick (one of) the overloading method(s) instead of the overloaded one.
Andreas_D
+4  A: 

Err... what's the point in not allowing to Overload the method?

Protection from Overriding is allowed because it can be done by another programmer who is supposedly working on another part of code and his class is inherited from your class.

Overloading is done in the same class and is logically supposed to be done by a programmer who knows this code and is working on the same part of the code. So, if he knows this code (theoretically) and there is some inherent danger in overloading, then he should know this already because he knows the code.

That said, overloading cannot be stopped as others have already described.

Aamir