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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:31:38+00:00 2026-06-16T02:31:38+00:00

I’m having trouble figuring out how to access a private instance variable of the

  • 0

I’m having trouble figuring out how to access a private instance variable of the super class.
I’m writing an equals method in the Dog class that compares to see if the name and breeds are the same, but name is a private instance variable inside Pet (that Dog inherits).

Here’s my code:

public class Pet {

    private String name;

    public Pet(){
        name = "";
    }
    public Pet(String name){
        this.name = name;
    }

    public boolean equals(Pet other){
        return this.name.equals(other.name);
    }
}

and my Dog class:

public class Dog extends Pet {
    private String breed;
    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }

    public Dog(){
        breed = "";
    }

    @Override
    public boolean equals(Object obj){

        if(obj == null){
            return false;
        }

        if(obj.getClass() != this.getClass()){
            return false;
        }else{
            Pet p = (Pet)obj;
            Pet q = (Pet)this;
            Dog temp = (Dog)obj;
            boolean name = q.equals(p);
            boolean bred = breed.equals(temp.breed);
            return name && bred;
        }
    }
}

In my main class:

Dog d1 = new Dog("Harry", "Puggle");
Dog d2 = new Dog("Harry", "Pug");
System.out.println(d1.equals(d2));

For some reason it keeps using my Pet class’s equal method.

Thanks

  • 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-16T02:31:40+00:00Added an answer on June 16, 2026 at 2:31 am

    @Pshemo has identified the immediate cause of your problem. Your Pet.equals(Object) does not override `Dog.equals(String) because the signatures don’t match. And your d1.equals(d2) call is binding to the most closely matching method signature, which is the one with a Pet formal parameter rather than an Object formal parameter.

    But once you have corrected that, there is another problem in the Dog.equals(String) method:

            Pet p = (Pet)obj;
            Pet q = (Pet)this;
            boolean name = q.equals(p);
    

    When you fix the signature of Pet.equals, that is going to result in a recursive call to Dog.equals … and a StackOverflowError. (Dog.equals will call Dog.equals, which will call Dog.equals, which …). Basically, q.equals is the same method as the one that is currently executing. The type casts aren’t doing anything …

    Change it to this:

            Pet p = (Pet)obj;
            boolean name = super.equals(p);
    

    The super keyword used this way invokes the overridden version of the equals method.


    I’m having trouble figuring out how to access a private instance variable of the super class.

    This is a different issue to what is causing your problem. But the answer is that if you want a child classes method to be able to access a private variable in a parent class, then you need to either add getter and/or setter methods to the parent class, or change the access of the variable to (typically) protected.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.