Reputation: 1150
I have a base class with age and name as instance members and derived class with bonus. I am overriding equals in Derived class. I know how equals works in Java when there is a single base class. But I am not able to understand how it works in case of inheritance. I want to check if two derived objects are equal.
I was expecting the output to be
This class = Base, Other class = Derived
instead the output is
This class = Derived, Other class = Derived
What is super in equals method of derived class exactly doing?Isnt it referring to Base?
<br/>
public class Base{
private int age;
private String name;
public Base(int age, String name){
this.age = age;
this.name = name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
@Override
public boolean equals(Object otherBase){
//Check if both the references refer to same object
if(this == otherBase){
return true;
}
if(otherBase == null){
return false;
}
System.out.println("This class ="+this.getClass().getCanonicalName()+", Other class = "+otherBase.getClass().getCanonicalName());
if(this.getClass() != otherBase.getClass()){
return false;
}
if(! (otherBase instanceof Base)){
return false;
}
Base other = (Base) otherBase;
return this.age == other.age && this.name.equals(other.name);
}
public static void main(String[] args){
Derived d1 = new Derived(10,6,"shri");
Derived d2 = new Derived(10,5, "shri");
if(d1.equals(d2)){
System.out.println("Equal");
}else{
System.out.println("Not Equal");
}
}
}
class Derived extends Base{
private int bonus;
public int getBonus(){
return bonus;
}
public Derived(int bonus, int age, String name){
super(age, name);
this.bonus = bonus;
}
@Override
public boolean equals(Object otherDerived){
if(!(super.equals(otherDerived))){
return false;
}
Derived derived = (Derived) otherDerived;
return bonus == derived.bonus;
}
}
Upvotes: 1
Views: 3532
Reputation: 72874
What is super in equals method of derived class exactly doing?Isnt it referring to Base?
super
is used to call the overriden equals method (the one defined in Base
). More generally, super
references the superclass of the type in which it is used, so in this case yes it refers to Base
. However, the referenced object's type at runtime is still Derived
.
In the Base#equals()
method, the expression this.getClass().getCanonicalName()
returns the name of the class for the object at runtime. Even if the expression is called in the equals
method of the base class, the actual type is Derived
. This is what the getClass
method does as mentioned in the Javadocs:
Returns the runtime class of this Object.
If you ever need to get the name of the superclass, you can use this.getClass().getSuperclass().getCanonicalName()
.
Upvotes: 1
Reputation: 9707
So the equals()
method is implemented correctly and it works in the following way:
age
and name
to the Base
classbonus
field in the Derived
classThe call of super.equals()
will call the equals()
from super class (Base
), but this
still represents the real instance, e.g. Derived
in your case.
Upvotes: 1