相关文章推荐
喝醉的小笼包  ·  动态 线程池 ...·  2 年前    · 
失落的啄木鸟  ·  ImportError: cannot ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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; This test if(! (otherBase instanceof Base)){ is unnecessary because of the one above it: if(this.getClass() != otherBase.getClass()) You've already made sure it is exactly the same class, so it can't not be a descendant of Base weston Mar 14, 2015 at 18:31
  • delegate the comparison of the age and name to the Base class
  • if it is not the same, return false
  • otherwise compare the value of the bonus field in the Derived class
  • The 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.

    So basically calling super.equals(otherDerived) is as good as d1 object calling equals method of Base class. Is that what you mean? user2938723 Mar 14, 2015 at 18:22

    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() .

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question . Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers .