<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
inheritance	O
is	O
the	O
mechanism	O
of	O
basing	O
an	O
object	O
or	O
class	O
upon	O
another	O
object	O
(	O
prototype-based	B-Application
inheritance	I-Application
)	O
or	O
class	O
(	O
class-based	B-Application
inheritance	I-Application
)	O
,	O
retaining	O
similar	O
implementation	O
.	O
</s>
<s>
Also	O
defined	O
as	O
deriving	O
new	O
classes	O
(	O
sub	O
classes	O
)	O
from	O
existing	O
ones	O
such	O
as	O
super	O
class	O
or	O
base	B-Application
class	I-Application
and	O
then	O
forming	O
them	O
into	O
a	O
hierarchy	O
of	O
classes	O
.	O
</s>
<s>
In	O
most	O
class-based	B-Application
object-oriented	B-Language
languages	I-Language
,	O
an	O
object	O
created	O
through	O
inheritance	O
,	O
a	O
"	O
child	O
object	O
"	O
,	O
acquires	O
all	O
the	O
properties	O
and	O
behaviors	O
of	O
the	O
"	O
parent	O
object	O
"	O
,	O
with	O
the	O
exception	O
of	O
:	O
constructors	O
,	O
destructor	O
,	O
overloaded	O
operators	O
and	O
friend	B-Language
functions	I-Language
of	O
the	O
base	B-Application
class	I-Application
.	O
</s>
<s>
Inheritance	O
allows	O
programmers	O
to	O
create	O
classes	O
that	O
are	O
built	O
upon	O
existing	O
classes	O
,	O
to	O
specify	O
a	O
new	O
implementation	O
while	O
maintaining	O
the	O
same	O
behaviors	O
(	O
realizing	O
an	O
interface	B-Application
)	O
,	O
to	O
reuse	O
code	O
and	O
to	O
independently	O
extend	O
original	O
software	O
via	O
public	O
classes	O
and	O
interfaces	O
.	O
</s>
<s>
The	O
term	O
"	O
inheritance	O
"	O
is	O
loosely	O
used	O
for	O
both	O
class-based	B-Application
and	O
prototype-based	B-Application
programming	I-Application
,	O
but	O
in	O
narrow	O
use	O
the	O
term	O
is	O
reserved	O
for	O
class-based	B-Application
programming	I-Application
(	O
one	O
class	O
inherits	O
from	O
another	O
)	O
,	O
with	O
the	O
corresponding	O
technique	O
in	O
prototype-based	B-Application
programming	I-Application
being	O
instead	O
called	O
delegation	B-Language
(	O
one	O
object	O
delegates	O
to	O
another	O
)	O
.	O
</s>
<s>
Class-modifying	O
inheritance	O
patterns	O
can	O
be	O
pre-defined	O
according	O
to	O
simple	O
network	O
interface	B-Application
parameters	O
such	O
that	O
inter-language	O
compatibility	O
is	O
preserved	O
.	O
</s>
<s>
In	O
some	O
languages	O
inheritance	O
and	O
subtyping	O
agree	O
,	O
whereas	O
in	O
others	O
they	O
differ	O
;	O
in	O
general	O
,	O
subtyping	O
establishes	O
an	O
is-a	O
relationship	O
,	O
whereas	O
inheritance	O
only	O
reuses	O
implementation	O
and	O
establishes	O
a	O
syntactic	O
relationship	O
,	O
not	O
necessarily	O
a	O
semantic	O
relationship	O
(	O
inheritance	O
does	O
not	O
ensure	O
behavioral	B-Application
subtyping	I-Application
)	O
.	O
</s>
<s>
To	O
distinguish	O
these	O
concepts	O
,	O
subtyping	O
is	O
sometimes	O
referred	O
to	O
as	O
interface	B-Application
inheritance	O
(	O
without	O
acknowledging	O
that	O
the	O
specialization	O
of	O
type	O
variables	O
also	O
induces	O
a	O
subtyping	O
relation	O
)	O
,	O
whereas	O
inheritance	O
as	O
defined	O
here	O
is	O
known	O
as	O
implementation	O
inheritance	O
or	O
code	O
inheritance	O
.	O
</s>
<s>
Inheritance	O
is	O
contrasted	O
with	O
object	B-Language
composition	I-Language
,	O
where	O
one	O
object	O
contains	O
another	O
object	O
(	O
or	O
objects	O
of	O
one	O
class	O
contain	O
objects	O
of	O
another	O
class	O
)	O
;	O
see	O
composition	B-Library
over	I-Library
inheritance	I-Library
.	O
</s>
<s>
Composition	O
implements	O
a	O
has-a	B-Application
relationship	O
,	O
in	O
contrast	O
to	O
the	O
is-a	O
relationship	O
of	O
subtyping	O
.	O
</s>
<s>
The	O
attributes	O
of	O
a	O
compound	O
object	O
would	O
be	O
accessible	O
by	O
dot	B-Language
notation	I-Language
.	O
</s>
<s>
This	O
idea	O
was	O
first	O
adopted	O
in	O
the	O
Simula	B-Language
67	I-Language
programming	O
language	O
.	O
</s>
<s>
The	O
idea	O
then	O
spread	O
to	O
Smalltalk	B-Application
,	O
C++	B-Language
,	O
Java	B-Language
,	O
Python	B-Language
,	O
and	O
many	O
other	O
languages	O
.	O
</s>
<s>
The	O
class	O
A	O
serves	O
as	O
a	O
base	B-Application
class	I-Application
for	O
the	O
derived	O
class	O
B	O
,	O
which	O
in	O
turn	O
serves	O
as	O
a	O
base	B-Application
class	I-Application
for	O
the	O
derived	O
class	O
C	O
.	O
The	O
class	O
B	O
is	O
known	O
as	O
intermediate	O
base	B-Application
class	I-Application
because	O
it	O
provides	O
a	O
link	O
for	O
the	O
inheritance	O
between	O
A	O
and	O
C	O
.	O
The	O
chain	O
ABC	O
is	O
known	O
as	O
inheritance	O
path	O
.	O
</s>
<s>
This	O
is	O
where	O
one	O
class	O
serves	O
as	O
a	O
superclass	O
(	O
base	B-Application
class	I-Application
)	O
for	O
more	O
than	O
one	O
sub	O
class	O
.	O
</s>
<s>
Subclasses	O
,	O
derived	O
classes	O
,	O
heir	O
classes	O
,	O
or	O
child	O
classes	O
are	O
modular	B-Architecture
derivative	O
classes	O
that	O
inherits	O
one	O
or	O
more	O
language	O
entities	O
from	O
one	O
or	O
more	O
other	O
classes	O
(	O
called	O
superclass	O
,	O
base	O
classes	O
,	O
or	O
parent	O
classes	O
)	O
.	O
</s>
<s>
The	O
semantics	O
of	O
class	O
inheritance	O
vary	O
from	O
language	O
to	O
language	O
,	O
but	O
commonly	O
the	O
subclass	O
automatically	O
inherits	O
the	O
instance	B-Application
variables	I-Application
and	O
member	O
functions	O
of	O
its	O
superclasses	O
.	O
</s>
<s>
Visibility	O
specifies	O
whether	O
the	O
features	O
of	O
the	O
base	B-Application
class	I-Application
are	O
privately	O
derived	O
or	O
publicly	O
derived	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Eiffel	B-Language
,	O
contracts	B-Application
that	O
define	O
the	O
specification	O
of	O
a	O
class	O
are	O
also	O
inherited	O
by	O
heirs	O
.	O
</s>
<s>
The	O
superclass	O
establishes	O
a	O
common	O
interface	B-Application
and	O
foundational	O
functionality	O
,	O
which	O
specialized	O
subclasses	O
can	O
inherit	O
,	O
modify	O
,	O
and	O
supplement	O
.	O
</s>
<s>
The	O
software	O
inherited	O
by	O
a	O
subclass	O
is	O
considered	O
reused	B-Application
in	O
the	O
subclass	O
.	O
</s>
<s>
The	O
actual	O
class	O
of	O
the	O
object	O
being	O
referenced	O
is	O
impossible	O
to	O
predict	O
at	O
compile-time	B-Application
.	O
</s>
<s>
A	O
uniform	O
interface	B-Application
is	O
used	O
to	O
invoke	O
the	O
member	O
functions	O
of	O
objects	O
of	O
a	O
number	O
of	O
different	O
classes	O
.	O
</s>
<s>
Examples	O
include	O
the	O
final	O
keyword	O
in	O
Java	B-Language
and	O
C++11	B-Language
onwards	O
or	O
the	O
sealed	O
keyword	O
in	O
C#	B-Application
.	O
</s>
<s>
Such	O
non-subclassable	O
classes	O
restrict	O
reusability	B-Application
,	O
particularly	O
when	O
developers	O
only	O
have	O
access	O
to	O
precompiled	O
binaries	O
and	O
not	O
source	O
code	O
.	O
</s>
<s>
A	O
non-subclassable	O
class	O
has	O
no	O
subclasses	O
,	O
so	O
it	O
can	O
be	O
easily	O
deduced	O
at	O
compile	B-Application
time	I-Application
that	O
references	O
or	O
pointers	O
to	O
objects	O
of	O
that	O
class	O
are	O
actually	O
referencing	O
instances	O
of	O
that	O
class	O
and	O
not	O
instances	O
of	O
subclasses	O
(	O
they	O
do	O
n't	O
exist	O
)	O
or	O
instances	O
of	O
superclasses	O
(	O
upcasting	O
a	O
reference	O
type	O
violates	O
the	O
type	O
system	O
)	O
.	O
</s>
<s>
Because	O
the	O
exact	O
type	O
of	O
the	O
object	O
being	O
referenced	O
is	O
known	O
before	O
execution	O
,	O
early	O
binding	O
(	O
also	O
called	O
static	B-Language
dispatch	I-Language
)	O
can	O
be	O
used	O
instead	O
of	O
late	O
binding	O
(	O
also	O
called	O
dynamic	O
dispatch	O
)	O
,	O
which	O
requires	O
one	O
or	O
more	O
virtual	B-Language
method	I-Language
table	I-Language
lookups	O
depending	O
on	O
whether	O
multiple	B-Application
inheritance	I-Application
or	O
only	O
single	O
inheritance	O
are	O
supported	O
in	O
the	O
programming	O
language	O
that	O
is	O
being	O
used	O
.	O
</s>
<s>
A	O
private	O
method	O
is	O
un-overridable	O
simply	O
because	O
it	O
is	O
not	O
accessible	O
by	O
classes	O
other	O
than	O
the	O
class	O
it	O
is	O
a	O
member	O
function	O
of	O
(	O
this	O
is	O
not	O
true	O
for	O
C++	B-Language
,	O
though	O
)	O
.	O
</s>
<s>
A	O
final	O
method	O
in	O
Java	B-Language
,	O
a	O
sealed	O
method	O
in	O
C#	B-Application
or	O
a	O
frozen	O
feature	O
in	O
Eiffel	B-Language
cannot	O
be	O
overridden	O
.	O
</s>
<s>
If	O
the	O
superclass	O
method	O
is	O
a	O
virtual	B-Application
method	I-Application
,	O
then	O
invocations	O
of	O
the	O
superclass	O
method	O
will	O
be	O
dynamically	O
dispatched	O
.	O
</s>
<s>
C++	B-Language
)	O
,	O
and	O
in	O
others	O
,	O
all	O
methods	O
are	O
virtual	O
(	O
e.g.	O
</s>
<s>
Java	B-Language
)	O
.	O
</s>
<s>
the	O
address	O
of	O
the	O
function	O
call	O
is	O
determined	O
at	O
compile-time	B-Application
)	O
.	O
</s>
<s>
Static	B-Language
dispatch	I-Language
is	O
faster	O
than	O
dynamic	O
dispatch	O
and	O
allows	O
optimizations	O
such	O
as	O
inline	O
expansion	O
.	O
</s>
<s>
The	O
following	O
table	O
shows	O
which	O
variables	O
and	O
functions	O
get	O
inherited	O
dependent	O
on	O
the	O
visibility	O
given	O
when	O
deriving	O
the	O
class	O
,	O
using	O
the	O
terminology	O
established	O
by	O
C++	B-Language
.	O
</s>
<s>
Many	O
object-oriented	B-Application
programming	I-Application
languages	I-Application
permit	O
a	O
class	O
or	O
object	O
to	O
replace	O
the	O
implementation	O
of	O
an	O
aspecttypically	O
a	O
behaviorthat	O
it	O
has	O
inherited	O
.	O
</s>
<s>
This	O
process	O
is	O
called	O
overriding	B-Language
.	O
</s>
<s>
Overriding	B-Language
introduces	O
a	O
complication	O
:	O
which	O
version	O
of	O
the	O
behavior	O
does	O
an	O
instance	O
of	O
the	O
inherited	O
class	O
usethe	O
one	O
that	O
is	O
part	O
of	O
its	O
own	O
class	O
,	O
or	O
the	O
one	O
from	O
the	O
parent	O
(	O
base	O
)	O
class	O
?	O
</s>
<s>
The	O
answer	O
varies	O
between	O
programming	O
languages	O
,	O
and	O
some	O
languages	O
provide	O
the	O
ability	O
to	O
indicate	O
that	O
a	O
particular	O
behavior	O
is	O
not	O
to	O
be	O
overridden	O
and	O
should	O
behave	O
as	O
defined	O
by	O
the	O
base	B-Application
class	I-Application
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
C#	B-Application
,	O
the	O
base	O
method	O
or	O
property	O
can	O
only	O
be	O
overridden	O
in	O
a	O
subclass	O
if	O
it	O
is	O
marked	O
with	O
the	O
virtual	O
,	O
abstract	O
,	O
or	O
override	B-Language
modifier	O
,	O
while	O
in	O
programming	O
languages	O
such	O
as	O
Java	B-Language
,	O
different	O
methods	O
can	O
be	O
called	O
to	O
override	B-Language
other	O
methods	O
.	O
</s>
<s>
An	O
alternative	O
to	O
overriding	B-Language
is	O
hiding	O
the	O
inherited	O
code	O
.	O
</s>
<s>
Implementation	O
inheritance	O
is	O
the	O
mechanism	O
whereby	O
a	O
subclass	O
re-uses	O
code	O
in	O
a	O
base	B-Application
class	I-Application
.	O
</s>
<s>
By	O
default	O
the	O
subclass	O
retains	O
all	O
of	O
the	O
operations	O
of	O
the	O
base	B-Application
class	I-Application
,	O
but	O
the	O
subclass	O
may	O
override	B-Language
some	O
or	O
all	O
operations	O
,	O
replacing	O
the	O
base-class	O
implementation	O
with	O
its	O
own	O
.	O
</s>
<s>
In	O
the	O
following	O
Python	B-Language
example	O
,	O
subclasses	O
and	O
override	B-Language
the	O
method	O
of	O
the	O
base	B-Application
class	I-Application
.	O
</s>
<s>
The	O
base	B-Application
class	I-Application
comprises	O
operations	O
to	O
compute	O
the	O
sum	O
of	O
the	O
squares	O
between	O
two	O
integers	O
.	O
</s>
<s>
The	O
subclass	O
re-uses	O
all	O
of	O
the	O
functionality	O
of	O
the	O
base	B-Application
class	I-Application
with	O
the	O
exception	O
of	O
the	O
operation	O
that	O
transforms	O
a	O
number	O
into	O
its	O
square	O
,	O
replacing	O
it	O
with	O
an	O
operation	O
that	O
transforms	O
a	O
number	O
into	O
its	O
square	O
and	O
cube	O
respectively	O
.	O
</s>
<s>
Below	O
is	O
an	O
example	O
of	O
Python	B-Language
.	O
</s>
<s>
The	O
primary	O
concern	O
is	O
that	O
implementation	O
inheritance	O
does	O
not	O
provide	O
any	O
assurance	O
of	O
polymorphic	B-Application
substitutability	B-Application
—	O
an	O
instance	O
of	O
the	O
reusing	O
class	O
cannot	O
necessarily	O
be	O
substituted	O
for	O
an	O
instance	O
of	O
the	O
inherited	O
class	O
.	O
</s>
<s>
An	O
alternative	O
technique	O
,	O
explicit	O
delegation	B-Language
,	O
requires	O
more	O
programming	O
effort	O
,	O
but	O
avoids	O
the	O
substitutability	B-Application
issue	O
.	O
</s>
<s>
In	O
C++	B-Language
private	O
inheritance	O
can	O
be	O
used	O
as	O
a	O
form	O
of	O
implementation	O
inheritance	O
without	O
substitutability	B-Application
.	O
</s>
<s>
Whereas	O
public	O
inheritance	O
represents	O
an	O
"	O
is-a	O
"	O
relationship	O
and	O
delegation	B-Language
represents	O
a	O
"	O
has-a	B-Application
"	O
relationship	O
,	O
private	O
(	O
and	O
protected	O
)	O
inheritance	O
can	O
be	O
thought	O
of	O
as	O
an	O
"	O
is	O
implemented	O
in	O
terms	O
of	O
"	O
relationship	O
.	O
</s>
<s>
Another	O
frequent	O
use	O
of	O
inheritance	O
is	O
to	O
guarantee	O
that	O
classes	O
maintain	O
a	O
certain	O
common	O
interface	B-Application
;	O
that	O
is	O
,	O
they	O
implement	O
the	O
same	O
methods	O
.	O
</s>
<s>
Often	O
,	O
there	O
is	O
no	O
interface	B-Application
change	O
between	O
the	O
supertype	O
and	O
subtype	O
-	O
the	O
child	O
implements	O
the	O
behavior	O
described	O
instead	O
of	O
its	O
parent	O
class	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
following	O
C++	B-Language
code	I-Language
establishes	O
an	O
explicit	O
inheritance	O
relationship	O
between	O
classes	O
B	O
and	O
A	O
,	O
where	O
B	O
is	O
both	O
a	O
subclass	O
and	O
a	O
subtype	O
of	O
A	O
,	O
and	O
can	O
be	O
used	O
as	O
an	O
A	O
wherever	O
a	O
B	O
is	O
specified	O
(	O
via	O
a	O
reference	O
,	O
a	O
pointer	O
or	O
the	O
object	O
itself	O
)	O
.	O
</s>
<s>
In	O
programming	O
languages	O
that	O
do	O
not	O
support	O
inheritance	O
as	O
a	O
subtyping	O
mechanism	O
,	O
the	O
relationship	O
between	O
a	O
base	B-Application
class	I-Application
and	O
a	O
derived	O
class	O
is	O
only	O
a	O
relationship	O
between	O
implementations	O
(	O
a	O
mechanism	O
for	O
code	O
reuse	O
)	O
,	O
as	O
compared	O
to	O
a	O
relationship	O
between	O
types	O
.	O
</s>
<s>
Inheritance	O
,	O
even	O
in	O
programming	O
languages	O
that	O
support	O
inheritance	O
as	O
a	O
subtyping	O
mechanism	O
,	O
does	O
not	O
necessarily	O
entail	O
behavioral	B-Application
subtyping	I-Application
.	O
</s>
<s>
It	O
is	O
entirely	O
possible	O
to	O
derive	O
a	O
class	O
whose	O
object	O
will	O
behave	O
incorrectly	O
when	O
used	O
in	O
a	O
context	O
where	O
the	O
parent	O
class	O
is	O
expected	O
;	O
see	O
the	O
Liskov	B-Application
substitution	I-Application
principle	I-Application
.	O
</s>
<s>
Using	O
multiple	B-Application
inheritance	I-Application
partially	O
solves	O
this	O
problem	O
,	O
as	O
one	O
can	O
then	O
define	O
a	O
class	O
that	O
inherits	O
from	O
both	O
and	O
.	O
</s>
<s>
The	O
inheritance	O
model	O
available	O
in	O
Eiffel	B-Language
makes	O
this	O
possible	O
through	O
support	O
for	O
repeated	O
inheritance	O
.	O
</s>
<s>
Many	O
modern	O
languages	O
,	O
including	O
C++	B-Language
and	O
Java	B-Language
,	O
provide	O
a	O
"	O
protected	O
"	O
access	O
modifier	O
that	O
allows	O
subclasses	O
to	O
access	O
the	O
data	O
,	O
without	O
allowing	O
any	O
code	O
outside	O
the	O
chain	O
of	O
inheritance	O
to	O
access	O
it	O
.	O
</s>
<s>
The	O
composite	B-Library
reuse	I-Library
principle	I-Library
is	O
an	O
alternative	O
to	O
inheritance	O
.	O
</s>
<s>
This	O
technique	O
supports	O
polymorphism	B-Application
and	O
code	O
reuse	O
by	O
separating	O
behaviors	O
from	O
the	O
primary	O
class	O
hierarchy	O
and	O
including	O
specific	O
behavior	O
classes	O
as	O
required	O
in	O
any	O
business	O
domain	O
class	O
.	O
</s>
<s>
Implementation	O
inheritance	O
is	O
controversial	O
among	O
programmers	O
and	O
theoreticians	O
of	O
object-oriented	B-Language
programming	I-Language
since	O
at	O
least	O
the	O
1990s	O
.	O
</s>
<s>
Among	O
them	O
are	O
the	O
authors	O
of	O
Design	O
Patterns	O
,	O
who	O
advocate	O
interface	B-Application
inheritance	O
instead	O
,	O
and	O
favor	O
composition	B-Library
over	I-Library
inheritance	I-Library
.	O
</s>
<s>
As	O
a	O
more	O
fundamental	O
solution	O
to	O
the	O
same	O
problem	O
,	O
role-oriented	B-General_Concept
programming	I-General_Concept
introduces	O
a	O
distinct	O
relationship	O
,	O
played-by	O
,	O
combining	O
properties	O
of	O
inheritance	O
and	O
composition	O
into	O
a	O
new	O
concept	O
.	O
</s>
<s>
According	O
to	O
Allen	O
Holub	O
,	O
the	O
main	O
problem	O
with	O
implementation	O
inheritance	O
is	O
that	O
it	O
introduces	O
unnecessary	O
coupling	B-Application
in	O
the	O
form	O
of	O
the	O
"	B-Application
fragile	I-Application
base	I-Application
class	I-Application
problem	I-Application
"	I-Application
:	O
modifications	O
to	O
the	O
base	B-Application
class	I-Application
implementation	O
can	O
cause	O
inadvertent	O
behavioral	O
changes	O
in	O
subclasses	O
.	O
</s>
<s>
Another	O
way	O
of	O
stating	O
this	O
is	O
that	O
"	O
inheritance	O
breaks	O
encapsulation	B-Application
"	O
.	O
</s>
<s>
The	O
problem	O
surfaces	O
clearly	O
in	O
open	O
object-oriented	B-Language
systems	O
such	O
as	O
frameworks	B-Architecture
,	O
where	O
client	O
code	O
is	O
expected	O
to	O
inherit	O
from	O
system-supplied	O
classes	O
and	O
then	O
substituted	O
for	O
the	O
system	O
's	O
classes	O
in	O
its	O
algorithms	O
.	O
</s>
<s>
Reportedly	O
,	O
Java	B-Language
inventor	O
James	O
Gosling	O
has	O
spoken	O
against	O
implementation	O
inheritance	O
,	O
stating	O
that	O
he	O
would	O
not	O
include	O
it	O
if	O
he	O
were	O
to	O
redesign	O
Java	B-Language
.	O
</s>
<s>
Language	O
designs	O
that	O
decouple	O
inheritance	O
from	O
subtyping	O
(	O
interface	B-Application
inheritance	O
)	O
appeared	O
as	O
early	O
as	O
1990	O
;	O
a	O
modern	O
example	O
of	O
this	O
is	O
the	O
Go	B-Application
programming	I-Application
language	I-Application
.	O
</s>
<s>
Complex	O
inheritance	O
,	O
or	O
inheritance	O
used	O
within	O
an	O
insufficiently	O
mature	O
design	O
,	O
may	O
lead	O
to	O
the	O
yo-yo	B-Application
problem	I-Application
.	O
</s>
