Composition over inheritance
Composite reuse principlecompositioncomposition instead of inheritance
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.wikipedia
34 Related Articles
Object-oriented programming
object-orientedobject orientedobject-oriented programming language
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.
The doctrine of composition over inheritance advocates implementing has-a relationships using composition instead of inheritance.


Inheritance (object-oriented programming)
inheritancesuperclasssubclass
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.
Inheritance is contrasted with object composition, where one object contains another object (or objects of one class contain objects of another class); see composition over inheritance.



Design Patterns
GoF design patternsDesign Patterns: Elements of Reusable Object-Oriented SoftwareGang of Four
This is an often-stated principle of OOP, such as in the influential book Design Patterns.

Object composition
compositionaggregationUser-defined type
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.

Go (programming language)
GoGo programming languageGolang
Some languages, notably Go, use type composition exclusively.
Of the omitted language features, the designers explicitly argue against assertions and pointer arithmetic, while defending the choice to omit type inheritance as giving a more useful language, encouraging instead the use of interfaces to achieve dynamic dispatch and composition to reuse code.

Forwarding (object-oriented programming)
forwardingforwardforwarding methods
One common drawback of using composition instead of inheritance is that methods being provided by individual components may have to be implemented in the derived type, even if they are only forwarding methods (this is true in most programming languages, but not all; see caveats.) In contrast, inheritance does not require all of the base class's methods to be re-implemented within the derived class.
Like inheritance, delegation allows the sending object to modify the original behavior, but is susceptible to problems analogous to the fragile base class; while forwarding provides stronger encapsulation and avoids these problems; see composition over inheritance.
Liskov substitution principle
Inheritance semanticssubstitutabilitybehavioral subtyping
Strategy pattern
Strategystrategy design pattern
The strategy pattern uses composition instead of inheritance.



Object-oriented design
designobject orientedobject-oriented
Code reuse
software reusereusere-use
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.
Interface (computing)
interfaceinterfacescomputer interface
An implementation of composition over inheritance typically begins with the creation of various interfaces representing the behaviors that the system must exhibit.
Polymorphism (computer science)
polymorphismpolymorphictype polymorphism
Interfaces enable polymorphic behavior.
Business domain
Classes implementing the identified interfaces are built and added to business domain classes as needed.
C++11
C++0xmove constructorconstexpr
An example in C++11 follows:
Multiple inheritance
diamond problemmulti-inheritanceDiamond inheritance problem
Note that multiple inheritance is dangerous if not implemented carefully, as it can lead to the diamond problem.
Virtual inheritance
Virtual Inheritance (object-oriented programming)Virtual base class
Keep in mind that C++ solves the diamond problem of multiple inheritance by allowing virtual inheritance.
Has-a
has anhas-a relationship
In other words, it is better to compose what an object can do (HAS-A) than extend what it is (IS-A).

Is-a
is asubsumptionis-a'' hierarchies
In other words, it is better to compose what an object can do (HAS-A) than extend what it is (IS-A).
Trait (computer programming)
traitsTraitTraits class
This drawback can be avoided by using traits, mixins, (type) embedding, or protocol extensions.
Mixin
mixinsinter-type declarationsmixed in
This drawback can be avoided by using traits, mixins, (type) embedding, or protocol extensions.
Embedding
embeddedisometric embeddingtopological embedding
This drawback can be avoided by using traits, mixins, (type) embedding, or protocol extensions.
Protocol (object-oriented programming)
interfaceinterfacesprotocols
This drawback can be avoided by using traits, mixins, (type) embedding, or protocol extensions.
Java (programming language)
JavaJava programming languageJava language




Swift (programming language)
SwiftSwift 3Swift programming language