<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
in	O
languages	O
such	O
as	O
C++	B-Language
,	O
and	O
Object	B-Language
Pascal	I-Language
,	O
a	O
virtual	B-Application
function	I-Application
or	O
virtual	B-Application
method	I-Application
is	O
an	O
inheritable	O
and	O
overridable	B-Language
function	O
or	O
method	B-Language
for	O
which	O
dynamic	O
dispatch	O
is	O
facilitated	O
.	O
</s>
<s>
This	O
concept	O
is	O
an	O
important	O
part	O
of	O
the	O
(	O
runtime	B-Library
)	O
polymorphism	B-Application
portion	O
of	O
object-oriented	B-Language
programming	I-Language
(	O
OOP	O
)	O
.	O
</s>
<s>
In	O
short	O
,	O
a	O
virtual	B-Application
function	I-Application
defines	O
a	O
target	O
function	O
to	O
be	O
executed	O
,	O
but	O
the	O
target	O
might	O
not	O
be	O
known	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
Most	O
programming	O
languages	O
,	O
such	O
as	O
JavaScript	B-Language
,	O
PHP	B-Application
and	O
Python	B-Language
,	O
treat	O
all	O
methods	O
as	O
virtual	O
by	O
default	O
and	O
do	O
not	O
provide	O
a	O
modifier	O
to	O
change	O
this	O
behavior	O
.	O
</s>
<s>
However	O
,	O
some	O
languages	O
provide	O
modifiers	O
to	O
prevent	O
methods	O
from	O
being	O
overridden	O
by	O
derived	O
classes	O
(	O
such	O
as	O
the	O
final	O
keyword	O
in	O
Java	B-Language
and	O
PHP	B-Application
)	O
.	O
</s>
<s>
The	O
concept	O
of	O
the	O
virtual	B-Application
function	I-Application
solves	O
the	O
following	O
problem	O
:	O
</s>
<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
when	O
a	O
derived	O
class	O
inherits	O
from	O
a	O
base	O
class	O
,	O
an	O
object	O
of	O
the	O
derived	O
class	O
may	O
be	O
referred	O
to	O
via	O
a	O
pointer	O
or	O
reference	O
of	O
the	O
base	O
class	O
type	O
instead	O
of	O
the	O
derived	O
class	O
type	O
.	O
</s>
<s>
If	O
there	O
are	O
base	O
class	O
methods	O
overridden	O
by	O
the	O
derived	O
class	O
,	O
the	O
method	B-Language
actually	O
called	O
by	O
such	O
a	O
reference	O
or	O
pointer	O
can	O
be	O
bound	O
(	O
linked	O
)	O
either	O
'	O
early	O
 '	O
(	O
by	O
the	O
compiler	O
)	O
,	O
according	O
to	O
the	O
declared	O
type	O
of	O
the	O
pointer	O
or	O
reference	O
,	O
or	O
'	O
late	O
 '	O
(	O
i.e.	O
,	O
by	O
the	O
runtime	B-Device
system	I-Device
of	O
the	O
language	O
)	O
,	O
according	O
to	O
the	O
actual	O
type	O
of	O
the	O
object	O
is	O
referred	O
to	O
.	O
</s>
<s>
Virtual	B-Application
functions	I-Application
are	O
resolved	O
'	O
late	O
 '	O
.	O
</s>
<s>
If	O
the	O
function	O
in	O
question	O
is	O
'	O
virtual	O
 '	O
in	O
the	O
base	O
class	O
,	O
the	O
most-derived	O
class	O
's	O
implementation	B-Application
of	O
the	O
function	O
is	O
called	O
according	O
to	O
the	O
actual	O
type	O
of	O
the	O
object	O
referred	O
to	O
,	O
regardless	O
of	O
the	O
declared	O
type	O
of	O
the	O
pointer	O
or	O
reference	O
.	O
</s>
<s>
If	O
it	O
is	O
not	O
'	O
virtual	O
 '	O
,	O
the	O
method	B-Language
is	O
resolved	O
'	O
early	O
 '	O
and	O
selected	O
according	O
to	O
the	O
declared	O
type	O
of	O
the	O
pointer	O
or	O
reference	O
.	O
</s>
<s>
Virtual	B-Application
functions	I-Application
allow	O
a	O
program	O
to	O
call	O
methods	O
that	O
do	O
n't	O
necessarily	O
even	O
exist	O
at	O
the	O
moment	O
the	O
code	O
is	O
compiled	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
virtual	B-Application
methods	I-Application
are	O
declared	O
by	O
prepending	O
the	O
keyword	O
to	O
the	O
function	O
's	O
declaration	O
in	O
the	O
base	O
class	O
.	O
</s>
<s>
This	O
modifier	O
is	O
inherited	O
by	O
all	O
implementations	O
of	O
that	O
method	B-Language
in	O
derived	O
classes	O
,	O
meaning	O
that	O
they	O
can	O
continue	O
to	O
over-ride	O
each	O
other	O
and	O
be	O
late-bound	O
.	O
</s>
<s>
And	O
even	O
if	O
methods	O
owned	O
by	O
the	O
base	O
class	O
call	O
the	O
virtual	B-Application
method	I-Application
,	O
they	O
will	O
instead	O
be	O
calling	O
the	O
derived	O
method	B-Language
.	O
</s>
<s>
Overloading	B-Application
occurs	O
when	O
two	O
or	O
more	O
methods	O
in	O
one	O
class	O
have	O
the	O
same	O
method	B-Language
name	I-Language
but	O
different	O
parameters	O
.	O
</s>
<s>
Overriding	B-Language
means	O
having	O
two	O
methods	O
with	O
the	O
same	O
method	B-Language
name	I-Language
and	O
parameters	O
.	O
</s>
<s>
Overloading	B-Application
is	O
also	O
referred	O
to	O
as	O
function	O
matching	O
,	O
and	O
overriding	B-Language
as	O
dynamic	O
function	O
mapping	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
base	O
class	O
Animal	O
could	O
have	O
a	O
virtual	B-Application
function	I-Application
Eat	O
.	O
</s>
<s>
In	O
C	O
,	O
the	O
mechanism	O
behind	O
virtual	B-Application
functions	I-Application
could	O
be	O
provided	O
in	O
the	O
following	O
manner	O
:	O
</s>
<s>
A	O
pure	B-Application
virtual	I-Application
function	I-Application
or	O
pure	B-Application
virtual	I-Application
method	I-Application
is	O
a	O
virtual	B-Application
function	I-Application
that	O
is	O
required	O
to	O
be	O
implemented	O
by	O
a	O
derived	O
class	O
if	O
the	O
derived	O
class	O
is	O
not	O
abstract	O
.	O
</s>
<s>
Classes	O
containing	O
pure	B-Application
virtual	I-Application
methods	I-Application
are	O
termed	O
"	O
abstract	O
"	O
and	O
they	O
cannot	O
be	O
instantiated	O
directly	O
.	O
</s>
<s>
A	O
subclass	O
of	O
an	O
abstract	O
class	O
can	O
only	O
be	O
instantiated	O
directly	O
if	O
all	O
inherited	O
pure	B-Application
virtual	I-Application
methods	I-Application
have	O
been	O
implemented	O
by	O
that	O
class	O
or	O
a	O
parent	O
class	O
.	O
</s>
<s>
Pure	B-Application
virtual	I-Application
methods	I-Application
typically	O
have	O
a	O
declaration	O
(	O
signature	O
)	O
and	O
no	O
definition	O
(	O
implementation	B-Application
)	O
.	O
</s>
<s>
As	O
an	O
example	O
,	O
an	O
abstract	O
base	O
class	O
MathSymbol	O
may	O
provide	O
a	O
pure	B-Application
virtual	I-Application
function	I-Application
doOperation( )	O
,	O
and	O
derived	O
classes	O
Plus	O
and	O
Minus	O
implement	O
doOperation( )	O
to	O
provide	O
concrete	O
implementations	O
.	O
</s>
<s>
Although	O
pure	B-Application
virtual	I-Application
methods	I-Application
typically	O
have	O
no	O
implementation	B-Application
in	O
the	O
class	O
that	O
declares	O
them	O
,	O
pure	B-Application
virtual	I-Application
methods	I-Application
in	O
some	O
languages	O
(	O
e.g.	O
</s>
<s>
C++	B-Language
and	O
Python	B-Language
)	O
are	O
permitted	O
to	O
contain	O
an	O
implementation	B-Application
in	O
their	O
declaring	O
class	O
,	O
providing	O
fallback	O
or	O
default	O
behaviour	O
that	O
a	O
derived	O
class	O
can	O
delegate	O
to	O
,	O
if	O
appropriate	O
.	O
</s>
<s>
Pure	B-Application
virtual	I-Application
functions	I-Application
can	O
also	O
be	O
used	O
where	O
the	O
method	B-Language
declarations	O
are	O
being	O
used	O
to	O
define	O
an	O
interface	B-Language
-	O
similar	O
to	O
what	O
the	O
interface	B-Language
keyword	O
in	O
Java	B-Language
explicitly	O
specifies	O
.	O
</s>
<s>
In	O
such	O
a	O
design	O
pattern	O
,	O
the	O
abstract	O
class	O
which	O
serves	O
as	O
an	O
interface	B-Language
will	O
contain	O
only	O
pure	B-Application
virtual	I-Application
functions	I-Application
,	O
but	O
no	O
data	O
members	O
or	O
ordinary	O
methods	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
using	O
such	O
purely	O
abstract	O
classes	O
as	O
interfaces	O
works	O
because	O
C++	B-Language
supports	O
multiple	B-Application
inheritance	I-Application
.	O
</s>
<s>
However	O
,	O
because	O
many	O
OOP	O
languages	O
do	O
not	O
support	O
multiple	B-Application
inheritance	I-Application
,	O
they	O
often	O
provide	O
a	O
separate	O
interface	B-Language
mechanism	O
.	O
</s>
<s>
An	O
example	O
is	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
Languages	O
differ	O
in	O
their	O
behavior	O
while	O
the	O
constructor	O
or	O
destructor	B-Language
of	O
an	O
object	O
is	O
running	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
calling	O
virtual	B-Application
functions	I-Application
in	O
constructors	O
is	O
generally	O
discouraged	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
the	O
"	O
base	O
"	O
function	O
is	O
called	O
.	O
</s>
<s>
Specifically	O
,	O
the	O
most	O
derived	O
function	O
that	O
is	O
not	O
more	O
derived	O
than	O
the	O
current	O
constructor	O
or	O
destructor	B-Language
's	O
class	O
is	O
called	O
.	O
</s>
<s>
If	O
that	O
function	O
is	O
a	O
pure	B-Application
virtual	I-Application
function	I-Application
,	O
then	O
undefined	B-Language
behavior	I-Language
occurs	O
.	O
</s>
<s>
This	O
is	O
true	O
even	O
if	O
the	O
class	O
contains	O
an	O
implementation	B-Application
for	O
that	O
pure	B-Application
virtual	I-Application
function	I-Application
,	O
since	O
a	O
call	O
to	O
a	O
pure	B-Application
virtual	I-Application
function	I-Application
must	O
be	O
explicitly	O
qualified	O
.	O
</s>
<s>
A	O
conforming	O
C++	B-Language
implementation	B-Application
is	O
not	O
required	O
(	O
and	O
generally	O
not	O
able	O
)	O
to	O
detect	O
indirect	O
calls	O
to	O
pure	B-Application
virtual	I-Application
functions	I-Application
at	O
compile	B-Application
time	I-Application
or	O
link	B-Application
time	I-Application
.	O
</s>
<s>
Some	O
runtime	B-Device
systems	I-Device
will	O
issue	O
a	O
pure	B-Application
virtual	I-Application
function	I-Application
call	O
error	O
when	O
encountering	O
a	O
call	O
to	O
a	O
pure	B-Application
virtual	I-Application
function	I-Application
at	O
run	B-Library
time	I-Library
.	O
</s>
<s>
In	O
Java	B-Language
and	O
C#	O
,	O
the	O
derived	O
implementation	B-Application
is	O
called	O
,	O
but	O
some	O
fields	O
are	O
not	O
yet	O
initialized	O
by	O
the	O
derived	O
constructor	O
(	O
although	O
they	O
are	O
initialized	O
to	O
their	O
default	O
zero	O
values	O
)	O
.	O
</s>
<s>
Some	O
design	O
patterns	O
,	O
such	O
as	O
the	O
Abstract	B-Language
Factory	I-Language
Pattern	I-Language
,	O
actively	O
promote	O
this	O
usage	O
in	O
languages	O
supporting	O
this	O
ability	O
.	O
</s>
<s>
Object-oriented	B-Language
languages	I-Language
typically	O
manage	O
memory	O
allocation	O
and	O
de-allocation	O
automatically	O
when	O
objects	O
are	O
created	O
and	O
destroyed	O
.	O
</s>
<s>
However	O
,	O
some	O
object-oriented	B-Language
languages	I-Language
allow	O
a	O
custom	O
destructor	B-Language
method	B-Language
to	O
be	O
implemented	O
,	O
if	O
desired	O
.	O
</s>
<s>
If	O
the	O
language	O
in	O
question	O
uses	O
automatic	O
memory	O
management	O
,	O
the	O
custom	O
destructor	B-Language
(	O
generally	O
called	O
a	O
finalizer	O
in	O
this	O
context	O
)	O
that	O
is	O
called	O
is	O
certain	O
to	O
be	O
the	O
appropriate	O
one	O
for	O
the	O
object	O
in	O
question	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
an	O
object	O
of	O
type	O
Wolf	O
that	O
inherits	O
Animal	O
is	O
created	O
,	O
and	O
both	O
have	O
custom	O
destructors	B-Language
,	O
the	O
one	O
called	O
will	O
be	O
the	O
one	O
declared	O
in	O
Wolf	O
.	O
</s>
<s>
In	O
manual	O
memory	O
management	O
contexts	O
,	O
the	O
situation	O
can	O
be	O
more	O
complex	O
,	O
particularly	O
in	O
relation	O
to	O
static	B-Language
dispatch	I-Language
.	O
</s>
<s>
If	O
an	O
object	O
of	O
type	O
Wolf	O
is	O
created	O
but	O
pointed	O
to	O
by	O
an	O
Animal	O
pointer	O
,	O
and	O
it	O
is	O
this	O
Animal	O
pointer	O
type	O
that	O
is	O
deleted	O
,	O
the	O
destructor	B-Language
called	O
may	O
actually	O
be	O
the	O
one	O
defined	O
for	O
Animal	O
and	O
not	O
the	O
one	O
for	O
Wolf	O
,	O
unless	O
the	O
destructor	B-Language
is	O
virtual	O
.	O
</s>
<s>
This	O
is	O
particularly	O
the	O
case	O
with	O
C++	B-Language
,	O
where	O
the	O
behavior	O
is	O
a	O
common	O
source	O
of	O
programming	O
errors	O
if	O
destructors	B-Language
are	O
not	O
virtual	O
.	O
</s>
