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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:14:23+00:00 2026-06-04T13:14:23+00:00

Specifically, why would a variable declared in both a parent and child class be

  • 0

Specifically, why would a variable declared in both a parent and child class be invisible in an instance of the child class passed to a method where the argument is the parent class?

Here is a simplified example of an issue that’s puzzling me. There are three classes, a parent and two child subtypes. The classes have a Foo object as a field and this static sort of factory method:

class Parent {
    public Foo myFoo;

    public static ThinChild createThinChild(Foo someFoo) {
        ThinChild thinChild = new ThinChild(someFoo);
        return thinChild;
    }

    public Foo getFoo() {
        return myFoo;
    }
}

class ThinChild extends Parent {
    public Foo myFoo;

    public ThinChild(Foo someFoo) {
        myFoo = someFoo;
    }
}

class ThickChild extends Parent {
    public Foo myFoo;

    public ThickChild(Foo someFoo) {
        myFoo = someFoo;
    }
}

Now consider this handler class:

class ChildHandler {
    private void doSomethingToThinChild(ThinChild thinChild) {
        assert(thinChild.getFoo() != null);
        doSomethingToAllChildren(thinChild);
    }

    private void doSomethingToAllChildren(Parent thinOrThickChild) {
        assert(thinOrThickChild.getFoo() != null);
    }
}

When I call doSomethingToThinChild, the assert passes. But in the method doSomethingToAllChildren, the assert fails. This is not what I expect.

Moreover, if I add the same exact getFoo method that’s in the Parent class to my ThinChild class, the assert now works. Which solves my immediate problem but seems undesirable to me insofar as I’m now maintaining identical code in each of the child classes where I’d prefer just to maintain it in the parent.

My actual case is a bit more complicated, but I hope I’ve captured all the relevant details. Assuming that I have, is there any explanation for why this would be expected behavior?

It is possible that there’s an additional complication in my actual code that I haven’t represented here. But I’d first like to confirm that my understanding of class inheritance in Java is sound.

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-04T13:14:24+00:00Added an answer on June 4, 2026 at 1:14 pm

    Remove public Foo myFoo. In nested classes, this field extends from parent class. You can’t change it because you have 2 variables with same name. So JVM initialized the variable with the smaller scope.

    Also the good way is to create constructor in parent:

    public Parent(Foo someFoo) {
        myFoo = someFoo;
    }
    

    and then call it in subclasses:

    public ThinChild(Foo someFoo) {
        super(someFoo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a method to retrieve the file name of a class? Specifically I
I would like to know if it is acceptable/preferred to use self::method() and parent::method()
I'd like to be able to Alt-Click an instance variable (or a method) as
A good example of when exactly to use interfaces specifically in Java would be
I would ask a question regarding compilers, specifically how they work. I would believe
I would be making changes to Tomcat's 7 sever.xml file specifically port 8443 to
How would you succinctly assert the equality of Collection elements, specifically a Set in
I would like to create a simple designer which looks like visual studio. Specifically,
I would like to find a replacement for list.com, specifically the ability to accept
I would like to use XPath 2.0, more specifically the distinct-values function. I downloaded

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.