Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:06:32+00:00 2026-06-13T17:06:32+00:00

I have a simple base class, which is later extended by many separate classes,

  • 0

I have a simple base class, which is later extended by many separate classes, which potentially introduce new fields, but not necessarily. I defined an equals method in the base class, but also overriden that for a few subclasses. Is it OK to mix definitions in base/subclasses? In my case it was to avoid code duplication checking the same fields.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:06:33+00:00Added an answer on June 13, 2026 at 5:06 pm

    Take a look at “Implementing equals() To Allow Mixed-Type Comparison” from Angelika Langer .

    Here is a brief explanation of some problems and a possible solution:

    The equals contract says (amongst others):

    It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

    That means you might get problems if your sub class is introducing new fields and you’re comparing an object of the base class (or another sub class that doesn’t override equals) to an object of this sub class.

    Do NOT do the following:

    class BaseClass {
        private int field1 = 0;
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof BaseClass) {
                return field1 == ((BaseClass) obj).field1;
            }
            return false;
        }
    }
    
    class BadSubClass extends BaseClass {
        private int field2 = 0;
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof BadSubClass) {
                return super.equals(obj) 
                        && field2 == ((BadSubClass) obj).field2;
            }
            return false;
        }
    }
    

    because you get

    BaseClass baseClass = new BaseClass();
    BadSubClass subClass = new BadSubClass();
    
    System.out.println(baseClass.equals(subClass)); // prints 'true'
    System.out.println(subClass.equals(baseClass)); // prints 'false'
    

    A possible solution:

    Replace the instanceof-check with a class comparison:

    obj != null && obj.getClass() == getClass()
    

    With this solution an object of BaseClass will never be equal to an object of any subclass.

    If you create another SubClass without an @Override of the equals method, two SubClass-objects can be equal to each other (if the BaseClass.equals check decides so) out of the box, but a SubClass-object will never be equal to a BaseClass-object.

    A good implementation could be as follows:

    class BaseClass {
        private int field1 = 0;
    
        @Override
        public boolean equals(Object obj) {
            if (obj != null && obj.getClass() == getClass()) {
                return field1 == ((BaseClass) obj).field1;
            }
            return false;
        }
    }
    
    class GoodSubClass extends BaseClass {
        private int field2 = 0;
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof GoodSubClass) {
                return super.equals(obj) && field2 == ((GoodSubClass) obj).field2;
            }
            return false;
        }
    }
    

    Please refer to the article mentioned above for more advanced problems and their solutions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple class with width and height member fields which define number of
This is a rather simple question, I have a base class which implements common
I have simple base class with single static field. I have numerous classes that
Suppose I have the following (trivially simple) base class: public class Simple { public
I have a simple server in Sinatra, like require 'sinatra/base' class Server < Sinatra::Base
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
the question is simple. I have a base abstract class (person). From this i
I have the following simple class class base { public: int x; base &set(int
I have a base class Element and derived two other classes from it, Solid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.