<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
specifically	O
object-oriented	B-Language
programming	I-Language
,	O
a	O
class	B-Application
invariant	I-Application
(	O
or	O
type	O
invariant	B-Application
)	O
is	O
an	O
invariant	B-Application
used	O
for	O
constraining	O
objects	O
of	O
a	O
class	O
.	O
</s>
<s>
Methods	B-Language
of	O
the	O
class	O
should	O
preserve	O
the	O
invariant	B-Application
.	O
</s>
<s>
The	O
class	B-Application
invariant	I-Application
constrains	O
the	O
state	O
stored	O
in	O
the	O
object	O
.	O
</s>
<s>
Class	B-Application
invariants	I-Application
are	O
established	O
during	O
construction	O
and	O
constantly	O
maintained	O
between	O
calls	O
to	O
public	O
methods	B-Language
.	O
</s>
<s>
Code	O
within	O
functions	O
may	O
break	O
invariants	B-Application
as	O
long	O
as	O
the	O
invariants	B-Application
are	O
restored	O
before	O
a	O
public	O
function	O
ends	O
.	O
</s>
<s>
With	O
concurrency	B-Architecture
,	O
maintaining	O
the	O
invariant	B-Application
in	O
methods	B-Language
typically	O
requires	O
a	O
critical	O
section	O
to	O
be	O
established	O
by	O
locking	B-Operating_System
the	O
state	O
using	O
a	O
mutex	B-Operating_System
.	O
</s>
<s>
An	O
object	B-Application
invariant	I-Application
,	O
or	O
representation	O
invariant	B-Application
,	O
is	O
a	O
computer	B-General_Concept
programming	I-General_Concept
construct	O
consisting	O
of	O
a	O
set	O
of	O
invariant	B-Application
properties	O
that	O
remain	O
uncompromised	O
regardless	O
of	O
the	O
state	O
of	O
the	O
object	O
.	O
</s>
<s>
This	O
ensures	O
that	O
the	O
object	O
will	O
always	O
meet	O
predefined	O
conditions	O
,	O
and	O
that	O
methods	B-Language
may	O
,	O
therefore	O
,	O
always	O
reference	O
the	O
object	O
without	O
the	O
risk	O
of	O
making	O
inaccurate	O
presumptions	O
.	O
</s>
<s>
Defining	O
class	B-Application
invariants	I-Application
can	O
help	O
programmers	O
and	O
testers	O
to	O
catch	O
more	O
bugs	O
during	O
software	O
testing	O
.	O
</s>
<s>
The	O
useful	O
effect	O
of	O
class	B-Application
invariants	I-Application
in	O
object-oriented	B-Language
software	O
is	O
enhanced	O
in	O
the	O
presence	O
of	O
inheritance	O
.	O
</s>
<s>
Class	B-Application
invariants	I-Application
are	O
inherited	O
,	O
that	O
is	O
,	O
"	O
the	O
invariants	B-Application
of	O
all	O
the	O
parents	O
of	O
a	O
class	O
apply	O
to	O
the	O
class	O
itself.	O
"	O
</s>
<s>
The	O
concern	O
for	O
this	O
type	O
of	O
misbehaving	O
descendant	O
is	O
one	O
reason	O
object-oriented	B-Language
software	O
designers	O
give	O
for	O
favoring	O
composition	B-Library
over	I-Library
inheritance	I-Library
(	O
i.e.	O
,	O
inheritance	O
breaks	O
encapsulation	O
)	O
.	O
</s>
<s>
However	O
,	O
because	O
class	B-Application
invariants	I-Application
are	O
inherited	O
,	O
the	O
class	B-Application
invariant	I-Application
for	O
any	O
particular	O
class	O
consists	O
of	O
any	O
invariant	B-Application
assertions	O
coded	O
immediately	O
on	O
that	O
class	O
in	O
conjunction	O
with	O
all	O
the	O
invariant	B-Application
clauses	O
inherited	O
from	O
the	O
class	O
's	O
parents	O
.	O
</s>
<s>
This	O
means	O
that	O
even	O
though	O
descendant	O
classes	O
may	O
have	O
access	O
to	O
the	O
implementation	O
data	O
of	O
their	O
parents	O
,	O
the	O
class	B-Application
invariant	I-Application
can	O
prevent	O
them	O
from	O
manipulating	O
those	O
data	O
in	O
any	O
way	O
that	O
produces	O
an	O
invalid	O
instance	O
at	O
runtime	O
.	O
</s>
<s>
Common	O
programming	O
languages	O
like	O
Python	O
,	O
PHP	O
,	O
JavaScript	O
,	O
C++	B-Language
and	O
Java	B-Language
support	O
assertions	O
by	O
default	O
,	O
which	O
can	O
be	O
used	O
to	O
define	O
class	B-Application
invariants	I-Application
.	O
</s>
<s>
A	O
common	O
pattern	O
to	O
implement	O
invariants	B-Application
in	O
classes	O
is	O
for	O
the	O
constructor	O
of	O
the	O
class	O
to	O
throw	O
an	O
exception	O
if	O
the	O
invariant	B-Application
is	O
not	O
satisfied	O
.	O
</s>
<s>
Since	O
methods	B-Language
preserve	O
the	O
invariants	B-Application
,	O
they	O
can	O
assume	O
the	O
validity	O
of	O
the	O
invariant	B-Application
and	O
need	O
not	O
explicitly	O
check	O
for	O
it	O
.	O
</s>
<s>
The	O
class	B-Application
invariant	I-Application
is	O
an	O
essential	O
component	O
of	O
design	B-Application
by	I-Application
contract	I-Application
.	O
</s>
<s>
So	O
,	O
programming	O
languages	O
that	O
provide	O
full	O
native	O
support	O
for	O
design	B-Application
by	I-Application
contract	I-Application
,	O
such	O
as	O
Eiffel	B-Language
,	O
Ada	B-Language
,	O
and	O
D	B-Application
,	O
will	O
also	O
provide	O
full	O
support	O
for	O
class	B-Application
invariants	I-Application
.	O
</s>
<s>
For	O
C++	B-Language
,	O
the	O
Loki	B-Language
Library	I-Language
provides	O
a	O
framework	O
for	O
checking	O
class	B-Application
invariants	I-Application
,	O
static	O
data	O
invariants	B-Application
,	O
and	O
exception	O
safety	O
.	O
</s>
<s>
For	O
Java	B-Language
,	O
there	O
is	O
a	O
more	O
powerful	O
tool	O
called	O
Java	B-Language
Modeling	I-Language
Language	I-Language
that	O
provides	O
a	O
more	O
robust	O
way	O
of	O
defining	O
class	B-Application
invariants	I-Application
.	O
</s>
<s>
The	O
Ada	B-Language
programming	I-Language
language	I-Language
has	O
native	O
support	O
for	O
type	O
invariants	B-Application
(	O
as	O
well	O
as	O
pre	O
-	O
and	O
postconditions	O
,	O
subtype	O
predicates	O
,	O
etc	O
.	O
)	O
.	O
</s>
<s>
A	O
type	O
invariant	B-Application
may	O
be	O
given	O
on	O
a	O
private	O
type	O
(	O
for	O
example	O
to	O
define	O
a	O
relationship	O
between	O
its	O
abstract	O
properties	O
)	O
,	O
or	O
on	O
its	O
full	O
definition	O
(	O
typically	O
to	O
help	O
in	O
verifying	O
the	O
correctness	O
of	O
the	O
implementation	O
of	O
the	O
type	O
)	O
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
a	O
type	O
invariant	B-Application
given	O
on	O
the	O
full	O
definition	O
of	O
a	O
private	O
type	O
used	O
to	O
represent	O
a	O
logical	O
stack	O
.	O
</s>
<s>
The	O
implementation	O
uses	O
an	O
array	O
,	O
and	O
the	O
type	O
invariant	B-Application
specifies	O
certain	O
properties	O
of	O
the	O
implementation	O
that	O
enable	O
proofs	O
of	O
safety	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
invariant	B-Application
ensures	O
that	O
,	O
for	O
a	O
stack	O
of	O
logical	O
depth	O
N	O
,	O
the	O
first	O
N	O
elements	O
of	O
the	O
array	O
are	O
valid	O
values	O
.	O
</s>
<s>
The	O
Default_Initial_Condition	O
of	O
the	O
Stack	O
type	O
,	O
by	O
specifying	O
an	O
empty	O
stack	O
,	O
ensures	O
the	O
initial	O
truth	O
of	O
the	O
invariant	B-Application
,	O
and	O
Push	O
preserves	O
the	O
invariant	B-Application
.	O
</s>
<s>
The	O
truth	O
of	O
the	O
invariant	B-Application
then	O
enables	O
Pop	O
to	O
rely	O
on	O
the	O
fact	O
that	O
the	O
top	O
of	O
the	O
stack	O
is	O
a	O
valid	O
value	O
,	O
which	O
is	O
needed	O
to	O
prove	O
Pop	O
's	O
postcondition	O
.	O
</s>
<s>
A	O
more	O
complex	O
type	O
invariant	B-Application
would	O
enable	O
the	O
proof	O
of	O
full	O
functional	O
correctness	O
,	O
such	O
as	O
that	O
Pop	O
returns	O
the	O
value	O
passed	O
into	O
a	O
corresponding	O
Push	O
,	O
but	O
in	O
this	O
case	O
we	O
are	O
merely	O
trying	O
to	O
prove	O
that	O
Pop	O
does	O
not	O
return	O
an	O
Invalid_Value	O
.	O
</s>
<s>
D	B-Application
programming	I-Application
language	I-Application
has	O
native	O
support	O
of	O
class	B-Application
invariants	I-Application
,	O
as	O
well	O
as	O
other	O
contract	B-Application
programming	I-Application
techniques	O
.	O
</s>
<s>
In	O
Eiffel	B-Language
,	O
the	O
class	B-Application
invariant	I-Application
appears	O
at	O
the	O
end	O
of	O
the	O
class	O
following	O
the	O
keyword	O
invariant	B-Application
.	O
</s>
<s>
The	O
Loki	B-Language
(	O
C++	B-Language
)	O
library	O
provides	O
a	O
framework	O
written	O
by	O
Richard	O
Sposato	O
for	O
checking	O
class	B-Application
invariants	I-Application
,	O
static	O
data	O
invariants	B-Application
,	O
and	O
exception	O
safety	O
level	O
.	O
</s>
<s>
This	O
is	O
an	O
example	O
of	O
how	O
class	O
can	O
use	O
Loki::Checker	O
to	O
verify	O
invariants	B-Application
remain	O
true	O
after	O
an	O
object	O
changes	O
.	O
</s>
<s>
The	O
geopoint	O
invariants	B-Application
are	O
:	O
</s>
<s>
This	O
is	O
an	O
example	O
of	O
a	O
class	B-Application
invariant	I-Application
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
with	O
Java	B-Language
Modeling	I-Language
Language	I-Language
.	O
</s>
<s>
Public	O
member	O
functions	O
should	O
define	O
precondition	O
and	O
postcondition	O
to	O
help	O
ensure	O
the	O
class	B-Application
invariant	I-Application
.	O
</s>
