I have 4 small classes below.
public class A
{
protected int _i;
public A(int i)
{
_i = i;
}
}
public class B extends A
{
public B(int i)
{
super(i+1);
}
}
public class D extends B
{
public D(int i)
{
super(i+1);
}
public boolean equals (D other)
{
return ((other!=null) && (_i==((D) other)._i));
}
}
This is the main code:
public class DriverABCD
{
public static void main (String [] args) {
D d = new D(1);
Object d1 = new D(1);
System.out.println (d1.equals(d));
}
}
The instance variable in the both classes should be set to "3". However, System.out.println (d1.equals(d));
returns FALSE. Only if I change the equals() method signature in class D to:
public boolean equals (Object other)
{
return ((other!=null) && (_i==((D) other)._i));
}
it will return TRUE. But why?