<s>
The	O
circle	B-Application
–	I-Application
ellipse	I-Application
problem	I-Application
in	O
software	O
development	O
(	O
sometimes	O
called	O
the	O
square	O
–	O
rectangle	O
problem	O
)	O
illustrates	O
several	O
pitfalls	O
which	O
can	O
arise	O
when	O
using	O
subtype	O
polymorphism	O
in	O
object	B-Application
modelling	I-Application
.	O
</s>
<s>
The	O
issues	O
are	O
most	O
commonly	O
encountered	O
when	O
using	O
object-oriented	B-Language
programming	I-Language
(	O
OOP	O
)	O
.	O
</s>
<s>
By	O
definition	O
,	O
this	O
problem	O
is	O
a	O
violation	O
of	O
the	O
Liskov	B-Application
substitution	I-Application
principle	I-Application
,	O
one	O
of	O
the	O
SOLID	O
principles	O
.	O
</s>
<s>
The	O
problem	O
concerns	O
which	O
subtyping	O
or	O
inheritance	B-Language
relationship	O
should	O
exist	O
between	O
classes	O
which	O
represent	O
circles	O
and	O
ellipses	O
(	O
or	O
,	O
similarly	O
,	O
squares	O
and	O
rectangles	O
)	O
.	O
</s>
<s>
More	O
generally	O
,	O
the	O
problem	O
illustrates	O
the	O
difficulties	O
which	O
can	O
occur	O
when	O
a	O
base	O
class	O
contains	O
methods	B-Language
which	O
mutate	O
an	O
object	O
in	O
a	O
manner	O
which	O
may	O
invalidate	O
a	O
(	O
stronger	O
)	O
invariant	O
found	O
in	O
a	O
derived	O
class	O
,	O
causing	O
the	O
Liskov	B-Application
substitution	I-Application
principle	I-Application
to	O
be	O
violated	O
.	O
</s>
<s>
The	O
existence	O
of	O
the	O
circle	B-Application
–	I-Application
ellipse	I-Application
problem	I-Application
is	O
sometimes	O
used	O
to	O
criticize	O
object-oriented	B-Language
programming	I-Language
.	O
</s>
<s>
It	O
is	O
a	O
central	O
tenet	O
of	O
object-oriented	B-Application
analysis	I-Application
and	I-Application
design	I-Application
that	O
subtype	O
polymorphism	O
,	O
which	O
is	O
implemented	O
in	O
most	O
object-oriented	B-Language
languages	I-Language
via	O
inheritance	B-Language
,	O
should	O
be	O
used	O
to	O
model	O
object	O
types	O
that	O
are	O
subsets	O
of	O
each	O
other	O
;	O
this	O
is	O
commonly	O
referred	O
to	O
as	O
the	O
is-a	O
relationship	O
.	O
</s>
<s>
Thus	O
,	O
code	O
written	O
in	O
an	O
object-oriented	B-Language
language	I-Language
that	O
models	O
shapes	O
will	O
frequently	O
choose	O
to	O
make	O
a	O
subclass	O
of	O
,	O
i.e.	O
</s>
<s>
A	O
subclass	O
must	O
provide	O
support	O
for	O
all	O
behaviour	O
supported	O
by	O
the	O
super-class	O
;	O
subclasses	O
must	O
implement	O
any	O
mutator	O
methods	B-Language
defined	O
in	O
a	O
base	O
class	O
.	O
</s>
<s>
A	O
related	O
problem	O
with	O
this	O
inheritance	B-Language
arises	O
when	O
considering	O
the	O
implementation	O
.	O
</s>
<s>
It	O
may	O
be	O
possible	O
to	O
avoid	O
this	O
if	O
the	O
language	O
(	O
such	O
as	O
Eiffel	B-Language
)	O
makes	O
constant	O
values	O
of	O
a	O
class	O
,	O
functions	O
without	O
arguments	O
,	O
and	O
data	O
members	O
interchangeable	O
.	O
</s>
<s>
Allow	O
the	O
objects	O
to	O
return	O
a	O
"	O
success	O
"	O
or	O
"	O
failure	O
"	O
value	O
for	O
each	O
modifier	O
or	O
raise	O
an	O
exception	B-General_Concept
on	O
failure	O
.	O
</s>
<s>
Alternately	O
,	O
could	O
throw	O
an	O
exception	B-General_Concept
(	O
but	O
depending	O
on	O
the	O
language	O
,	O
this	O
may	O
also	O
require	O
that	O
the	O
original	O
author	O
of	O
declare	O
that	O
it	O
may	O
throw	O
an	O
exception	B-General_Concept
)	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Common	B-Language
Lisp	I-Language
,	O
this	O
can	O
be	O
done	O
via	O
the	O
method	O
.	O
</s>
<s>
For	O
languages	O
that	O
allow	O
implicit	O
conversion	O
like	O
C++	B-Language
,	O
this	O
may	O
only	O
be	O
a	O
partial	O
solution	O
solving	O
the	O
problem	O
on	O
call-by-copy	O
,	O
but	O
not	O
on	O
call-by-reference	O
.	O
</s>
<s>
One	O
can	O
change	O
the	O
model	O
so	O
that	O
instances	O
of	O
the	O
classes	O
represent	O
constant	O
values	O
(	O
i.e.	O
,	O
they	O
are	O
immutable	B-Application
)	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
methods	B-Language
such	O
as	O
must	O
be	O
changed	O
to	O
yield	O
a	O
new	O
instance	O
,	O
rather	O
than	O
modifying	O
the	O
instance	O
they	O
act	O
on	O
.	O
</s>
<s>
This	O
means	O
that	O
it	O
is	O
no	O
longer	O
a	O
problem	O
to	O
define	O
,	O
and	O
the	O
inheritance	B-Language
reflects	O
the	O
mathematical	O
relationship	O
between	O
circles	O
and	O
ellipses	O
.	O
</s>
<s>
One	O
can	O
specify	O
that	O
is	O
only	O
allowed	O
on	O
instances	O
satisfying	O
,	O
and	O
will	O
otherwise	O
throw	O
an	O
exception	B-General_Concept
.	O
</s>
<s>
Create	O
an	O
abstract	O
base	O
class	O
called	O
and	O
put	O
methods	B-Language
that	O
work	O
with	O
both	O
s	O
and	O
s	O
in	O
this	O
class	O
.	O
</s>
<s>
Any	O
common	O
operations	O
desired	O
for	O
both	O
a	O
Circle	O
and	O
Ellipse	O
can	O
be	O
abstracted	O
out	O
to	O
a	O
common	O
interface	O
that	O
each	O
class	O
implements	O
,	O
or	O
into	O
mixins	B-Language
.	O
</s>
<s>
Also	O
,	O
one	O
may	O
provide	O
conversion	O
methods	B-Language
like	O
,	O
which	O
returns	O
a	O
mutable	B-Application
Ellipse	O
object	O
initialized	O
using	O
the	O
circle	O
's	O
radius	O
.	O
</s>
<s>
Methods	B-Language
converting	O
the	O
other	O
way	O
need	O
not	O
commit	O
to	O
one	O
strategy	O
.	O
</s>
<s>
There	O
is	O
no	O
reason	O
to	O
have	O
class	O
unless	O
it	O
needs	O
some	O
circle-specific	O
methods	B-Language
that	O
ca	O
n't	O
be	O
applied	O
to	O
an	O
ellipse	O
,	O
or	O
unless	O
the	O
programmer	O
wishes	O
to	O
benefit	O
from	O
conceptual	O
and/or	O
performance	O
advantages	O
of	O
the	O
circle	O
's	O
simpler	O
model	O
.	O
</s>
<s>
Majorinc	O
proposed	O
a	O
model	O
that	O
divides	O
methods	B-Language
on	O
modifiers	O
,	O
selectors	O
and	O
general	O
methods	B-Language
.	O
</s>
<s>
In	O
general	O
case	O
,	O
the	O
methods	B-Language
must	O
be	O
explicitly	O
inherited	O
.	O
</s>
<s>
The	O
model	O
can	O
be	O
emulated	O
in	O
languages	O
with	O
multiple	B-Application
inheritance	I-Application
,	O
using	O
abstract	O
classes	O
.	O
</s>
<s>
Essentially	O
,	O
the	O
circle	B-Application
–	I-Application
ellipse	I-Application
problem	I-Application
is	O
one	O
of	O
synchronizing	O
two	O
representations	O
of	O
type	O
:	O
the	O
de	O
facto	O
type	O
based	O
on	O
the	O
properties	O
of	O
the	O
object	O
,	O
and	O
the	O
formal	O
type	O
associated	O
with	O
the	O
object	O
by	O
the	O
object	B-Language
system	I-Language
.	O
</s>
<s>
It	O
is	O
clear	O
that	O
a	O
circle	O
cannot	O
satisfy	O
the	O
invariants	O
required	O
of	O
it	O
while	O
its	O
base	O
ellipse	O
methods	B-Language
allow	O
mutation	O
of	O
parameters	O
.	O
</s>
<s>
Many	O
object	B-Language
systems	I-Language
in	O
popular	O
use	O
are	O
based	O
on	O
a	O
design	O
which	O
takes	O
it	O
for	O
granted	O
that	O
an	O
object	O
carries	O
the	O
same	O
type	O
over	O
its	O
entire	O
lifetime	O
,	O
from	O
construction	O
to	O
finalization	O
.	O
</s>
<s>
The	O
following	O
example	O
uses	O
the	O
Common	B-Application
Lisp	I-Application
Object	I-Application
System	I-Application
(	O
CLOS	B-Application
)	O
in	O
which	O
objects	O
can	O
change	O
class	O
without	O
losing	O
their	O
identity	O
.	O
</s>
<s>
The	O
circle	O
and	O
ellipse	O
models	O
are	O
deliberately	O
simplified	O
to	O
avoid	O
distracting	O
details	O
which	O
are	O
not	O
relevant	O
to	O
the	O
circle	B-Application
–	I-Application
ellipse	I-Application
problem	I-Application
.	O
</s>
<s>
This	O
code	O
can	O
be	O
demonstrated	O
with	O
an	O
interactive	O
session	O
,	O
using	O
the	O
CLISP	O
implementation	O
of	O
Common	B-Language
Lisp	I-Language
.	O
</s>
<s>
This	O
strongly	O
suggests	O
that	O
inheritance	B-Language
should	O
never	O
be	O
used	O
when	O
the	O
sub-class	O
restricts	O
the	O
freedom	O
implicit	O
in	O
the	O
base	O
class	O
,	O
but	O
should	O
only	O
be	O
used	O
when	O
the	O
sub-class	O
adds	O
extra	O
detail	O
to	O
the	O
concept	O
represented	O
by	O
the	O
base	O
class	O
as	O
in	O
'	O
Monkey	O
 '	O
is-an	O
'	O
Animal	O
 '	O
.	O
</s>
<s>
One	O
may	O
,	O
conceptually	O
,	O
consider	O
a	O
and	O
to	O
both	O
be	O
mutable	B-Application
container	O
types	O
,	O
aliases	O
of	O
and	O
respectively	O
.	O
</s>
