How can I have a method with two parameters, with both parameters having the same concrete type?
For example,
boolean equals(Object a, Object b)
allows for a of any type and b of any type.
I want to force such that a and b have the same concrete type. I tried
<T> boolean equals(T a, T b)
and inputting a Date and a String to that method, expecting a compile-time error, but I get no errors, since T will resolve to ? extends Serializable & Comparable, since both Date and String implements Serializable and Comparable.
You can't, basically. There's no way of doing that. Even if you could do it for a simple call to prohibit arguments of different types, it could always be bypassed using a cast:
equals((Object) date, (Object) string)
If you're interested in the execution-time types of the arguments, you can only test that at execution time. There's no way of the compiler knowing whether an argument of type Date has a value which is a reference to precisely a java.util.Date or some subclass.
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