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 6699459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:41:51+00:00 2026-05-26T06:41:51+00:00

So the scenario is as follows: class Feline { String name; int age; equals(Object

  • 0

So the scenario is as follows:

class Feline  
{  
   String name;  
   int age;  

   equals(Object obj) {...}  
   hashCode(){...}  
}  


class Cat extends Feline  
{  
   int teeth;  

   hashCode(){...}  
   equals(Object obj)  
   {
     if (!super.equals(obj))
    {
        return false;   //If I don't want this should I use 
    }    
      ...
   }  
}

The issue is in reality this inheritance is correct, but to the program it is not necessarily true. My thought on this is Cat should actually be composed of a Feline object. The question is, which of these approaches should I take?

EDIT
This is the implementation from Eclipse, default equals/hashcode. It may be that the implementation of equals is not the most accurate way to do this.

  • 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-05-26T06:41:52+00:00Added an answer on May 26, 2026 at 6:41 am

    Ohoh equality checking in the face of inheritance. That’s EXTREMELY hard to get right and rather long to describe.

    The correct solution isn’t as straight forward as one would think, so please go read this – that should clear up all your questions. If not feel free to ask again 🙂

    Edit: So as a short summary of the above link: An equality method should fulfill the following properties:

    • It is reflexive: for any non-null value x, the expression x.equals(x) should return true.
    • It is symmetric: for any non-null values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    • It is transitive: for any non-null values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    • It is consistent: for any non-null values x and y, multiple invocations of x.equals(y) should consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
    • For any non-null value x, x.equals(null) should return false.

    To guarantee that this works, we need to specify another method: public boolean canEqual(Object other). This method should return true if the other object is an instance of the class in which canEqual is (re)defined, false otherwise.

    In other words the method must always be overwritten if we overwrite equal() itself. The implementation itself is trivial, a short example:

    class Foo {
        public boolean canEqual(Object other) {
            return other instanceof Foo;
        }
        // equals implementation, etc.
    }
    

    The equals method itself must always first check if the given object canEqual itself, e.g. something along the lines of other.canEqual(this) && restOfComparison.

    Short example of a class extending the above Foo:

    class Bar extends Foo {
        public boolean equals(Object other) {
            if (other instanceof Bar) {
                 Bar that = (Bar)other;
                 return that.canEqual(this) && otherStuff;
            }            
            return false;
        }
    
        public boolean canEqual(Object other) {
              return other instanceof Bar;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The scenario is as follows: Create an instance of a class (std::map) and sore
I have query regarding the disposing of an object. The scenario is as follows.
Edit Solved and Reposted as sample program The scenario was as follows: The class
The scenario is as follows. My Order model has an after_create that contacts a
The scenario is as follows, I have 3 objects (i simplified the names) named
My scenario is as follows. I have 7 tabs (one for each day) using
The architecture for this scenario is as follows: I have a table of items
If i have a @OneToMany relationship with @Cascade(CascadeType.SAVE_UPDATE) as follows public class One {
Lets say I have some view models set up as follows: public class User
I am having difficulties with listing this type of data. Scenario is as follows:

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.