<s>
Composition	B-Library
over	I-Library
inheritance	I-Library
(	O
or	O
composite	B-Library
reuse	I-Library
principle	I-Library
)	O
in	O
object-oriented	B-Language
programming	I-Language
(	O
OOP	O
)	O
is	O
the	O
principle	O
that	O
classes	O
should	O
achieve	O
polymorphic	B-Application
behavior	O
and	O
code	O
reuse	O
by	O
their	O
composition	B-Language
(	O
by	O
containing	O
instances	O
of	O
other	O
classes	O
that	O
implement	O
the	O
desired	O
functionality	O
)	O
rather	O
than	O
inheritance	B-Language
from	O
a	O
base	O
or	O
parent	O
class	O
.	O
</s>
<s>
An	O
implementation	O
of	O
composition	B-Library
over	I-Library
inheritance	I-Library
typically	O
begins	O
with	O
the	O
creation	O
of	O
various	O
interfaces	O
representing	O
the	O
behaviors	O
that	O
the	O
system	O
must	O
exhibit	O
.	O
</s>
<s>
Interfaces	O
can	O
facilitate	O
polymorphic	B-Application
behavior	O
.	O
</s>
<s>
Classes	O
implementing	O
the	O
identified	O
interfaces	O
are	O
built	O
and	O
added	O
to	O
business	B-General_Concept
domain	I-General_Concept
classes	O
as	O
needed	O
.	O
</s>
<s>
Thus	O
,	O
system	O
behaviors	O
are	O
realized	O
without	O
inheritance	B-Language
.	O
</s>
<s>
In	O
fact	O
,	O
business	B-General_Concept
domain	I-General_Concept
classes	O
may	O
all	O
be	O
base	O
classes	O
without	O
any	O
inheritance	B-Language
at	O
all	O
.	O
</s>
<s>
A	O
class	O
that	O
contains	O
a	O
reference	O
to	O
an	O
interface	O
can	O
support	O
implementations	O
of	O
the	O
interface	O
—	O
a	O
choice	O
that	O
can	O
be	O
delayed	O
until	O
runtime	B-Library
.	O
</s>
<s>
An	O
example	O
in	O
C++	B-Language
follows	O
:	O
</s>
<s>
Note	O
that	O
multiple	O
inheritance	B-Language
is	O
dangerous	O
if	O
not	O
implemented	O
carefully	O
because	O
it	O
can	O
lead	O
to	O
the	O
diamond	O
problem	O
.	O
</s>
<s>
C++	B-Language
uses	O
virtual	B-Application
inheritance	I-Application
to	O
solve	O
the	O
diamond	O
problem	O
of	O
multiple	O
inheritance	B-Language
.	O
</s>
<s>
The	O
C++	B-Language
examples	O
in	O
this	O
section	O
demonstrate	O
the	O
principle	O
of	O
using	O
composition	B-Language
and	O
interfaces	O
to	O
achieve	O
code	O
reuse	O
and	O
polymorphism	B-Application
.	O
</s>
<s>
Due	O
to	O
the	O
C++	B-Language
language	I-Language
not	O
having	O
a	O
dedicated	O
keyword	O
to	O
declare	O
interfaces	O
,	O
the	O
following	O
C++	B-Language
example	O
uses	O
inheritance	B-Language
from	O
a	O
pure	O
abstract	B-Application
base	I-Application
class	I-Application
.	O
</s>
<s>
For	O
most	O
purposes	O
,	O
this	O
is	O
functionally	O
equivalent	O
to	O
the	O
interfaces	O
provided	O
in	O
other	O
languages	O
,	O
such	O
as	O
Java	B-Language
and	O
C#	B-Application
.	O
</s>
<s>
To	O
favor	O
composition	B-Library
over	I-Library
inheritance	I-Library
is	O
a	O
design	O
principle	O
that	O
gives	O
the	O
design	O
higher	O
flexibility	O
.	O
</s>
<s>
Composition	B-Language
also	O
provides	O
a	O
more	O
stable	O
business	B-General_Concept
domain	I-General_Concept
in	O
the	O
long	O
term	O
as	O
it	O
is	O
less	O
prone	O
to	O
the	O
quirks	O
of	O
the	O
family	O
members	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
it	O
is	O
better	O
to	O
compose	O
what	O
an	O
object	O
can	O
do	O
(	O
has-a	B-Application
)	O
than	O
extend	O
what	O
it	O
is	O
(	O
is-a	O
)	O
.	O
</s>
<s>
Initial	O
design	O
is	O
simplified	O
by	O
identifying	O
system	O
object	O
behaviors	O
in	O
separate	O
interfaces	O
instead	O
of	O
creating	O
a	O
hierarchical	O
relationship	O
to	O
distribute	O
behaviors	O
among	O
business-domain	O
classes	O
via	O
inheritance	B-Language
.	O
</s>
<s>
This	O
approach	O
more	O
easily	O
accommodates	O
future	O
requirements	O
changes	O
that	O
would	O
otherwise	O
require	O
a	O
complete	O
restructuring	O
of	O
business-domain	O
classes	O
in	O
the	O
inheritance	B-Language
model	O
.	O
</s>
<s>
Additionally	O
,	O
it	O
avoids	O
problems	O
often	O
associated	O
with	O
relatively	O
minor	O
changes	O
to	O
an	O
inheritance-based	O
model	O
that	O
includes	O
several	O
generations	O
of	O
classes	O
.	O
</s>
<s>
Composition	B-Language
relation	O
is	O
more	O
flexible	O
as	O
it	O
may	O
be	O
changed	O
on	O
runtime	B-Library
,	O
while	O
sub-typing	O
relations	O
are	O
static	O
and	O
need	O
recompilation	O
in	O
many	O
languages	O
.	O
</s>
<s>
Some	O
languages	O
,	O
notably	O
Go	B-Application
and	O
Rust	B-Application
,	O
use	O
type	O
composition	B-Language
exclusively	O
.	O
</s>
<s>
One	O
common	O
drawback	O
of	O
using	O
composition	B-Language
instead	O
of	O
inheritance	B-Language
is	O
that	O
methods	O
being	O
provided	O
by	O
individual	O
components	O
may	O
have	O
to	O
be	O
implemented	O
in	O
the	O
derived	O
type	O
,	O
even	O
if	O
they	O
are	O
only	O
forwarding	B-Application
methods	I-Application
(	O
this	O
is	O
true	O
in	O
most	O
programming	O
languages	O
,	O
but	O
not	O
all	O
;	O
see	O
)	O
.	O
</s>
<s>
In	O
contrast	O
,	O
inheritance	B-Language
does	O
not	O
require	O
all	O
of	O
the	O
base	O
class	O
's	O
methods	O
to	O
be	O
re-implemented	O
within	O
the	O
derived	O
class	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
C#	B-Application
code	O
below	O
,	O
the	O
variables	O
and	O
methods	O
of	O
the	O
base	O
class	O
are	O
inherited	O
by	O
the	O
and	O
derived	O
subclasses	O
.	O
</s>
<s>
This	O
drawback	O
can	O
be	O
avoided	O
by	O
using	O
traits	O
,	O
mixins	B-Language
,	O
(	O
type	O
)	O
embedding	O
,	O
or	O
protocol	B-Application
extensions	O
.	O
</s>
<s>
C#	B-Application
provides	O
default	O
interface	O
methods	O
since	O
version	O
8.0	O
which	O
allows	O
to	O
define	O
body	O
to	O
interface	O
member	O
.	O
</s>
<s>
D	B-Application
provides	O
an	O
explicit	O
"	O
alias	O
this	O
"	O
declaration	O
within	O
a	O
type	O
can	O
forward	O
into	O
it	O
every	O
method	O
and	O
member	O
of	O
another	O
contained	O
type	O
.	O
</s>
<s>
Dart	B-Language
provides	O
mixins	B-Language
with	O
default	O
implementations	O
that	O
can	O
be	O
shared	O
.	O
</s>
<s>
Go	B-Application
type	O
embedding	O
avoids	O
the	O
need	O
for	O
forwarding	B-Application
methods	I-Application
.	O
</s>
<s>
Java	B-Language
provides	O
default	O
interface	O
methods	O
since	O
version	O
8	O
.	O
</s>
<s>
Julia	B-Application
macros	O
can	O
be	O
used	O
to	O
generate	O
forwarding	B-Application
methods	I-Application
.	O
</s>
<s>
Kotlin	B-Language
includes	O
the	O
delegation	B-Language
pattern	I-Language
in	O
the	O
language	O
syntax	O
.	O
</s>
<s>
PHP	B-Application
supports	O
traits	O
,	O
since	O
PHP	B-Application
5.4	O
.	O
</s>
<s>
Raku	B-Application
provides	O
a	O
trait	O
to	O
facilitate	O
method	O
forwarding	B-Application
.	O
</s>
<s>
Rust	B-Application
provides	O
traits	O
with	O
default	O
implementations	O
.	O
</s>
<s>
Scala	B-Application
(	O
since	O
version	O
3	O
)	O
provides	O
an	O
"	O
export	O
"	O
clause	O
to	O
define	O
aliases	O
for	O
selected	O
members	O
of	O
an	O
object	O
.	O
</s>
<s>
Swift	B-Application
extensions	O
can	O
be	O
used	O
to	O
define	O
a	O
default	O
implementation	O
of	O
a	O
protocol	B-Application
on	O
the	O
protocol	B-Application
itself	O
,	O
rather	O
than	O
within	O
an	O
individual	O
type	O
's	O
implementation	O
.	O
</s>
<s>
A	O
2013	O
study	O
of	O
93	O
open	O
source	O
Java	B-Language
programs	O
(	O
of	O
varying	O
size	O
)	O
found	O
that	O
:	O
</s>
