How does Java inheritance work when inner classes are involved

前端 未结 3 496
闹比i
闹比i 2021-01-04 09:56

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 ch

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 10:27

    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.

提交回复
热议问题