Comp401sp14lec06MoreEncaps Overloading
Comp401sp14lec06MoreEncaps Overloading
Encapsula.on
Overloading
COMP
401,
Spring
2014
Lecture
6
1/28/2013
lec6.ex1
null
references
The
value
null
is
valid
for
any
reference
type
variable
Really
means
no
value
at
all.
Or
in
other
words,
this
variable
doesnt
point
to
anything.
lec6.ex2
Why
immutability?
Can
be
shared
as
a
part
of
a
plurality
of
other
objects
without
danger.
Automa.cally
thread-safe
lec06.ex3
If
points
are
immutable,
then
triangle
class
does
not
have
to
worry
about
points
changing.
Related
to
the
principle
of
encapsula.on.
lec6.ex4
Polymorphism
Poly
=
many,
morph
=
forms
General
principle
of
providing
access
to
an
abstrac.on
or
method
in
many
forms
Idea
is
that
dierent
forms
t
dierent
contexts
Note:
underlying
func.onality
is
the
same.
Constructors
What
happens
when
you
dont
dene
a
constructor.
Default
constructor
with
no
arguments.
Creates
new
object
with
all
elds
set
to
default
value
Numeric
elds
set
to
0
Boolean
elds
set
to
false
String,
Array,
and
any
other
sort
of
reference
value
eld
set
to
null.
lec6.ex5.v1
Constructor
Overloading
Can
dene
mul.ple
versions
of
the
constructor.
Dis.nguished
from
each
other
by
type
and
number
of
parameters
Must
be
some
dierence
otherwise
the
compiler
wont
be
able
to
tell
them
apart.
lec6.ex5.v2
Constructor
Chaining
Common
paUern
is
to
chain
one
constructor
o
of
another.
First
line
of
code
in
the
constructor
must
be
the
this
keyword
as
a
func.on
with
parameters
Matching
constructor
is
called
rst
and
allowed
to
execute.
Then
remaining
code
in
original
constructor
called.
Can
chain
mul.ple
constructors
one
on
to
another
lec6.ex5.v3
Method
Overloading
Regular
methods
can
also
be
overloaded
lec5.ex5.v4, lec6.ex5.v5
Why
Overload?
Provides
access
to
constructor
/
method
in
a
more
context
specic
way.
Limita.ons
of
overloading
Does
not
handle
the
case
when
you
have
two
dierent
situa.ons
that
arent
dis.nguished
by
the
number
or
type
of
parameters
being
passed.