How to compare derived class object with Base class object java

How do I compare Base class object with derived class object in derived class? I need to get true as output from the code below:

class Base {
    int i;
    String str;

class Derived extends Base{
    double d;
    String str1;

    public static void main(String []a){
        Base b= new Base();
        Derived d = new Derived();
        System.out.println("Equals! :"+d.equals(b));

It always gives me false as output. How can I compare base class with derived class?

2 Answers
  1. Object equality is usually defined for objects of the same class, e.g.: You might want two objects Base b1, b2 to be equal if their str and i values are equal. Then you would need to define the equals() (and, typically, hashCode()) methods to test for this condition. For example:

    public boolean equals(Object obj)
        // test for identity
        if (this == obj) return true;
        // check that obj is not null, and that it is an instance of Base
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        // compare attributes
        Base other = (Base) obj;
        if (i != other.i) return false;
        if (str == null && other.str != null) return false;
        if (!str.equals(other.str)) return false;
        return true;

    In your case, since Base and Derived are different classes, with different attributes (Base has str and i, while Derived has str, i, str1, and d), you would need to define exactly when are a Base object and a Derived object supposed to be equal.

    2010-09-21 07:04:23
  2. Well, to make it just return true is easy enough:

    public boolean equals(Object other) {
        return true;

    However, I'd argue that that's not what you should be doing (even in a somewhat less trivial implementation).

    I think the equals method from Object shouldn't return true unless the types are actually the same. Otherwise it ends up making life really tricky in terms of making sure that the symmetry aspects required by Object.equals(Object) are fulfilled. It also doesn't feel like it's fulfilling natural equality at that point.

    However, it would be entirely reasonable to write an class which is able to compare any two instances of Base for equality in your specific case. The nice thing about equality comparators is that they don't have to be as natural - you're not defining what equality should mean for the whole type - you're just describing one specific comparison.

    It's unfortunate that Java doesn't have an EqualityComparator<T> (or whatever) interface to go with Comparator<T>. It makes it much harder to create (say) maps and sets which use a specific idea of equality (and a hash code to go with it). That may or may not be a problem in your case - how are you hoping to use this equality comparison anyway? If it's in your own code, you could write your own EqualityComparator<T> interface. If you're going to build a map or set, it's not going to be as easy :(

    2010-09-21 06:12:41
Related Articles
You Might Also Like