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

The Archive Base Latest Questions

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

I have a superclass with 2 instance variables (say int a , and int

  • 0

I have a superclass with 2 instance variables (say int a, and int b) and
have now decided I need to have one instance variable in my subclass int c.

I had overriden the superclass equals and hashCode method to take into account
equality of objects/values. I have also overriden the superclass toString to give a string
representing the state of the objects (variable values).

Based on the above and the fact I now have a single instance variable int c in my subclass
I was wondering if I

  1. need to override the toString method in the subclass or is this a no no as I have a valid inheritable toString method (already overriden) in the superclass that can be used for the subclass string representation?
  2. need to override the equals method in the subclass. Can this be done
    by calling the superclass equals method and adding the comparison code for my subclass instance variable after the call, or does it require a new implementation? I figure the latter?
  3. I was going to override the hashCode method in the subclass. Again is it best to have a new implementation than call the superclass hashCode method? I was going to
    compare a, b and c in the subclass equals method with another object and also create hash codes for a b and c in the subclass hashCode method? Is this the way to go or does it require me to only consider variable c in the subclass equals and hashcode methods?

I have had a look at various resources including Effective Java by bloch (chapter 3) but could not find the answer to the above questions that I have

  • 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-27T11:19:58+00:00Added an answer on May 27, 2026 at 11:19 am

    As long as everything in the subclass exists in the superclass, there is no need to override them (unless, of course, you want some different functionality).

    Basically, only override the method if you want different functionality for each subclass.

    But of course, you said that your subclass has a new variable, int c. In this case you would need to override the method to have complete functionality, especially for equals() and hashCode().

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

Sidebar

Related Questions

I have a situation where my subclass is not seeing the superclass' instance variable
I have a superclass and a subclass, both of which define instance variables. Rough
I have got two classes, one a subclass of the other (say Animal and
I have a Superclass with int a and int b. These variables are mutable.
I have this object which is an instance of a superclass. I want to
If I have a superclass, say Animal , and two subclasses: Zebra and Giraffe
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 want to have a function defined in a superclass that returns an instance
I have a class MyClass. It has instance variables passedInVar1, passedInVar2, etc. whose values

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.