I have the following code, the question is what equals
function will be called?
The answer is - the equals
of an Object class, can someone explain why?
I thought that the equals
of A class will be called since in runtime a is A and A has a proper equals
function for class A as b is a reference of class A.
Why my assumption is wrong?
public class MainClass
{
public static void main(String[] args)
{
Object a = new A();
A b = new B();
System.out.println(a.equals(b));
}
}
public class A
{
public boolean equals (A other)
{
System.out.println("a");
return true;
}
}
public class B extends A
{
public boolean equals (Object other)
{
System.out.println("b");
return true;
}
public boolean equals (B other)
{
System.out.println("c");
return true;
}
}