Suppose a method is as following:
void foo(int i)
{
}
Is there a way to make the following call illegal or generate an exception?
foo((short)3);
Maybe this?
class AClass {
void foo(int x) { /* do work */ }
void foo(short x) { throw new IllegalArgumentException(); }
}
Bruno's overloading will work, but if you are looking at preventing other types of casting, you can always box the integer into its Object class:
void foo(Integer i) {
// handle data normally
}
This will prevent you from being able to send short arguments to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With