Java Multiple Inheritance

前端 未结 17 1449
猫巷女王i
猫巷女王i 2020-11-22 09:36

In an attempt to fully understand how to solve Java\'s multiple inheritance problems I have a classic question that I need clarified.

Lets say I have class Ani

17条回答
  •  遥遥无期
    2020-11-22 10:23

    1. Define interfaces for defining the capabilities. You can define multiple interfaces for multiple capabilities. These capabilities can be implemented by specific Animal or Bird.
    2. Use inheritance to establish relationships among classes by sharing non-static and non-public data/methods.
    3. Use Decorator_pattern to add capabilities dynamically. This will allow you to reduce number of inheritance classes & combinations.

    Have a look at below example for better understanding

    When to Use the Decorator Pattern?

提交回复
热议问题