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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:04:27+00:00 2026-06-13T10:04:27+00:00

I am having trouble understanding how inheritance works in Java when inner classes are

  • 0

I am having trouble understanding how inheritance works in Java when inner classes are present. I’m currently working on something where a child class needs to slightly change the functionality of the inner class of it’s parent. I’ve come up with an simpler, analagous example below.

I expected this code to print “I am a ChildClass.InnerClass” but instead it prints
“I am a ParentClass.InnerClass”. Why is this? Also, if I change the obj object in main
to be of type ChildClass then the output changes to “I am a ChildClass.InnerClass”. Why is this?

In general, what is the recommended way of altering the behavior of an object’s parent class’s inner object?

class InnerClassTest {
   //-----------------------------------------------------------------------
   // PARENT CLASS
   class ParentClass {
      public ParentClass() {
         x = new InnerClass();
      }

      InnerClass x;

      class InnerClass {
         public void speak() {
            System.out.println("I am a ParentClass.InnerClass");
         }
      }
   }

   //-----------------------------------------------------------------------
   // CHILD CLASS
   class ChildClass extends ParentClass {
      public ChildClass() {
         x = new InnerClass();
      }

      InnerClass x;

      class InnerClass extends ParentClass.InnerClass {
         public void speak() {
            System.out.println("I am a ChildClass.InnerClass");
         }
      }
   }

   //-----------------------------------------------------------------------
   // MAIN
   public static void main(String[] args) {
      ParentClass obj = (new InnerClassTest()).new ChildClass();
      obj.x.speak();
   }
}
  • 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-13T10:04:28+00:00Added an answer on June 13, 2026 at 10:04 am

    Variable are not “overriden” as methods are.

    In your call, you expected x to be the Child‘s one but it isn’t because x is a variable, not a method.

    But pay attention: Your reference type is ParentClass so obj.x points to the ParentClass‘s InnerClass attribute even though the real instance behind parentClass is a ChildClass!

    In order to display your expected sentence, you have to change the type reference to ChildClass:

    public static void main(String[] args) {
          ChildClass obj = (new InnerClassTest()).new ChildClass();
          obj.x.speak();
    }
    

    To better understand the concept, try to define a method in both ParentClass and ChildClass classes:

    public InnerClass getInnerClass(){
      return x;
    }  
    

    and make x private.

    so that “override concept” applies.

    Your final call would be in this case:

    ParentClass obj = (new InnerClassTest()).new ChildClass();
    obj.getInnerClass().speak();
    

    To alter the behavior of the inner classes, think of Template method pattern or better: Strategy pattern (since more respectful of DIP)

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

Sidebar

Related Questions

Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
Im having trouble understanding class relationships after being asked to research it further, would
I'm having trouble understanding how to create relationships between classes on a project that
Im having trouble understanding this class and everything ive seen hasn't really helped. Ill
I am having trouble understanding how escaping works inside html tag attribute values that
I'm having trouble understanding how to get the following working in interface builder. I've
I am having trouble understanding this java code. I want the image to twinkle
I'm having trouble understanding how QGraphicsItemAnimation's setScaleAt function works. Here's the code I'm using:
I'm having trouble understanding the finer points of Java generics with wildcards. specifically, why
I'm having trouble understanding this behaviour of the Ruby Matrix class, is it just

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.