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

  • Home
  • SEARCH
  • 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 6956665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:53:57+00:00 2026-05-27T14:53:57+00:00

I have a Superclass with int a and int b. These variables are mutable.

  • 0

I have a Superclass with int a and int b. These variables are mutable. This superclass has 2 methods that can change the values of int a and int b for any object instances created from it.

I have a subclass inherited from the Superclass (extends) and also overrides the 2 superclass methods to add improved functionality that was lacking in the superclass.The subclass has 1 instance variable that is a unique id. I wanted to show the overriding of the equals and hashcode method for both the Superclass and subclass. It probable does not need to be done but I
was wanting to overide the equals and hashcode for one or both classes to suit my situation.

The superclass does not have a unique id instance variable. The superclass objects are unique and I wanted to show this in the equals and hashcode methods by comparing superclass int a and int b with instance of superclass int a and int b. Is this feasible? I did not want to use the default equals method which compares equality using the memory reference for two objects being compared.

I also wanted to show objects of the Subclass using Int c (unique id), int a and int b as unique
in the equals method.

can I do equality checks in both superclass equals and subclass equals methods, or is is a bit pointless? I did not want to use the default memory reference check but wanted to show equality or uniqueness using my 2 mutable int variables (superclass) and 1 unique id (subclass)

Do I or can I still need to test for equality if I override superclass equals and subclass equals knowing that the equality rules are broken?

Can I just override the supclass equals and hashcode methods, would that be best for my situation?

Any help much appreciated

  • 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-27T14:53:57+00:00Added an answer on May 27, 2026 at 2:53 pm

    There are basically two different philosophies here:

    a) sub extends super, so sub.equals() and sub.hashCode() both delegate to super.equals() and super.hashCode()

    b) each class is responsible for it’s own contract enforcements, sub.equals() and sub.hashCode() will consider parent fields by referencing them through the getters (super.getXyz()).

    Both of these approaches are valid. In approach a), super.equals() will check for instanceof compatibilty, in approach b) super.equals() will compare this.getClass() to that.getClass(). The problem with approach a) is that it will give you asymmetric equals() comparisons. In some cases sub.equals(super) will be false, but super.equals(sub) will be true.

    Read items 8 and 9 of Effective Java for more info.

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

Sidebar

Related Questions

I have this object which is an instance of a superclass. I want to
I have a superclass with 2 instance variables (say int a , and int
I have an AbstractEntity class as superclass for all my entites that defines an
I have a table of electronic devices that serves as a generic superclass and
I have an object of a subclass extending its superclass. There is an overridden
I have a subclass that calls a method from a superclass. The method in
I have several classes, that all derives from SuperClass. When the classes are created,
Suppose I have MyEdge and MyEdgeModified , such that MyEdge is the superclass and
Suppose I have this class: type Pet (name:string) as this = let mutable age
I have a function in my superclass ( Speler ) that is called kiesKaart

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.