P
puzzlecracker
public class Pair <T, U>
{
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || !(getClass().isInstance( o )) )
{
return false;
}
Pair<T, U> other = getClass().cast(o);
return (first == null? other.first == null :
first.equals( other.first ))
&& (second == null? other.second == null :
second.equals( other.second ));
}
}
This line gives a cast error during compilation: Pair<T, U> other =
getClass().cast(o);
Why can't we use a traditional Pair<T, U> other =(Pair<T, U> ) o; ?
Thanks
{
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || !(getClass().isInstance( o )) )
{
return false;
}
Pair<T, U> other = getClass().cast(o);
return (first == null? other.first == null :
first.equals( other.first ))
&& (second == null? other.second == null :
second.equals( other.second ));
}
}
This line gives a cast error during compilation: Pair<T, U> other =
getClass().cast(o);
Why can't we use a traditional Pair<T, U> other =(Pair<T, U> ) o; ?
Thanks