How does Java inheritance work when inner classes are involved

﹥>﹥吖頭↗ 提交于 2019-11-30 18:15:38

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)

Bhesh Gurung

Remove the redeclaration

InnerClass x; 

from the child class. So, that you will have only one x and will be reassigned in the constructor of the child class. Which means one x (refering to the object created in child ctor).

It's hiding the one in the parent class. Which is why you end up having two fields, refering to two different objects. And due to static (compile-time or early) binding in case of variables,

ParentClass obj; 
//obj.x means the x in parent

and

ChildClass obj; 
//obj.x means the x in child
millimoose

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

I'd recommend using a less convoluted design to begin with. A child class should modify the behaviour of its parent by overriding its methods, so I'd just add some factory method newInnerClass() to override the creation of this dependency, and manage this object at the top of the class hierarchy.

This would be more flexible than what you propose, because newInnerClass() could instantiate a class that's defined wherever as long as it has the right interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!