When is it Appropriate to use Generics Versus Inheritance?

前端 未结 7 730
傲寒
傲寒 2020-12-16 00:12

What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined?

Thanks for the answer

7条回答
  •  佛祖请我去吃肉
    2020-12-16 01:01

    Neither inheritance nor parameterized types can change at run-time

    Inheritance lets you provide default implementations for operations and lets subclasses override them.

    Parameterized types let you change the types that a class can use.

    But Which approach is best depends on your design and implementation constraints

    Design Patterns Elements of Reusable Object-Oriented Software

提交回复
热议问题