Beruflich Dokumente
Kultur Dokumente
SCJP
6
exam
prepara.on
course
by
Arian
CELINA,
B.Sc.,
SCJP
Chapter
7:
Object
Orienta,on
Contents
Encapsula.on
Inheritance
Polymorphism
Overriding/Overloading
Reference
variable
cas.ng
Implemen.ng
an
interface
Legal
return
types
Constructors
and
instan.a.on
Sta.cs
Coupling
and
Cohesion
Jan, 2009
Encapsula.on
The
benet
of
OO,
exibility
and
maintainability
Keep
your
variables/proper.es
private
Enforce
access
to
proper.es
with
geSer
and
seSer
methods
Keep
your
API
safe
for
modica.on
without
breaking
everyone
elses
work
API
Example
Jan, 2009
Inheritance
Jan, 2009
Inheritance
-
example
public
class
Human
extends
Moveable
{
System.out.println(A);
}
new Human().move();
new
Dog().move();
}
Jan,
2009
Is
A
rela.onship
Expressed
through
extends
and
implements
Jan, 2009
Is
A
rela.onship
-
example
class
Human{}
Boy is a Male
Boy is a Human
Male
is
a
Human
Boy
is
a
subclass
of
Male
Male
is
a
superclass
of
Boy
Human
is
a
superclass
of
Boy
Jan, 2009
Has
A
rela.onship
Object
A
is
said
to
have
Object
B
when
A
has
a
reference
to
B
public
class
Time
{
Hour h;
}
Time
Has
A
Hour
Jan,
2009
Polymorphism
Every
object
that
can
pass
more
than
one
Is-A
test
is
polymorphic
Except
objects
of
type
Object,
every
object
in
Java
is
polymorphic
Jan, 2009
Polymorphism
(2)
A
reference
variable
can
be
of
one
type,
and
that
type
cannot
change
aier
the
declara.on
A
reference
is
a
variable
and
it
can
be
re-assigned
Reference
variables
type
denes
which
methods
can
be
invoked
on
that
object
A
reference
variable
can
refer
to
any
object
of
declared
type
or
its
subtype
A
reference
type
can
be
declared
as
class
type
or
interface
type
Jan,
2009
10
Polymorphism
(3)
class
Human{}
11
Overriding
Overriding
means
re-implemen.ng
an
inherited
method
At
run.me,
regardless
of
the
variable
type,
overridden
method
will
run
public
class
Car
{
public
void
accelerate()
{
System.out.println(Generic
accelerate);
}
}
public
class
BMW
extends
Car
{
public
void
accelerate()
{
System.out.println(20
km\h
100
km\h);
}
}
Jan,
2009
12
Overriding
(2)
The
argument
list
must
be
same
with
the
overridden
methods
The
return
type
must
be
the
same,
or
a
subtype
The
access
level
can
not
be
more
restric.ve
can
be
less
restric.ve
A
method
can
be
overridden
only
if
it
is
inherited
Overriding
method
can
throw
any
unchecked
excep.on
Overriding
method
can
throw
a
narrower
excep.on,
but
not
wider
Overriding
method
can
not
throw
new
excep.ons
Jan,
2009
13
Overriding
(3)
Overriding
method
can
omit
declaring
an
excep.on
which
it
never
throws
even
overridden
does
declare
it
A
method
marked
nal
can
not
be
overridden
Sta.c
methods
can
not
be
overridden
Jan, 2009
14
Overloading
The
same
method
name
with
dierent
argument
list
Overloading
methods
MUST
change
the
arguments
Overloading
methods
CAN
change
the
return
type
Overloading
methods
CAN
throw
new
excep.ons
Overloading
methods
CAN
change
the
access
level
Overloading
can
be
in
the
same
class
or
in
a
subclass
if
overloaded
method
is
inherited
Jan,
2009
15
Overloading (2)
Jan, 2009
16
Overloading
(3)
class
Calculator
{
int
add(int
x,
int
y)
{
return
x
+
y;}
double
add(double
x,
double
y)
{
return
x
+
y;}
}
add(4,
6)
-
which
method
is
invoked?
add(2.2,
5.1)
which
method
is
invoked?
Jan, 2009
17
Overloading
(4)
class
Gree.ng{
void
gree.ng(Human
h)
{
System.out.println(Hello
human);}
void
gree.ng(Boy
b)
{
System.out.println(Hello
boy);}
}
Human
h
=
new
Human();
Boy
b
=
new
Boy();
Human
hb
=
new
Boy();
Calling:
gree.ng(h);
gree.ng(b);
what
will
be
print
in
each
of
these?
gree.ng
(hb);
Jan,
2009
18
Overloading
(4)
class
Gree.ng{
void
gree.ng(Human
h)
{
System.out.println(Hello
human);}
void
gree.ng(Boy
b)
{
System.out.println(Hello
boy);}
}
Human
h
=
new
Human();
Boy
b
=
new
Boy();
Human
hb
=
new
Boy();
Calling:
gree.ng(h);
------>
Hello
human
gree.ng(b);
------>
Hello
boy
gree.ng
(hb);
------>
Hello
human
Jan,
2009
19
void talk() {
System.out.println(Human talk);
new Human(),
new Boy()}
h.talk();
if (h instanceof Boy) {
((Boy)h).cry();
}
}
Jan,
2009
20
tree
Jan, 2009
21
Jan, 2009
22
Jan, 2009
23
Implemen.ng
an
interface
Implemen.ng
an
interface
is
complying
with
the
contract
The
concrete
class
which
implements
an
interface
must
provide
implementa.on
to
all
interface
methods
Follow
overriding
rules
Maintain
the
correct
signature
of
interface
methods
If
a
superclass
declares
it
implements
an
interface,
the
subclass
can
declare
it
again
but
not
provide
an
implementa.on.
Jan,
2009
24
Jan, 2009
25
Jan, 2009
26
27
Jan, 2009
28
Jan, 2009
29
Jan, 2009
30
Jan, 2009
31
Jan, 2009
32
Jan, 2009
33
class Test {
Test()
{}
Test(int
i)
{}
}
Jan,
2009
34
Sta.cs
Methods
are
marked
sta.c
if
its
implementa.on
is
not
dependant
to
instance
A
variable
is
marked
sta.c
if
it
should
keep
informa.on
related
to
class,
not
the
instance
e.g.
count
the
number
of
instances
Jan, 2009
35
Sta.cs
(2)
Sta.c
methods
cannot
be
overridden
Sta.c
methods
can
be
overloaded
Sta.c
methods
cannot
access
instance
variables
Sta.c
methods
can
use
sta.c
variables
and
other
sta.c
methods
Jan, 2009
36
Sta.cs
(3)
A
variable
is
marked
sta.c
if
it
should
keep
informa.on
related
to
class,
not
the
instance
e.g.
count
the
number
of
instances
A
method
is
marked
sta.c
if
it
does
not
used
instance
variables
and
non
sta.c
methods
of
the
class
Jan, 2009
37
Sta.cs
(4)
Accessing
sta.c
methods
and
variables
syntax
MyClassName.method()
Jan, 2009
OR
MyClassName.variable
38
Coupling
is
about
how
much
one
class
knows
about
the
other
Loose
coupling
is
good
Tight
coupling
is
bad
Cohesion
is
about
how
specialised
a
class
is
High
cohesion
facilitates
maintainability
of
the
code
Jan, 2009
39
References
SCJP
Sun
Cer.ed
Programmer
for
Java
6
Exam
310-065
By
Katherine
Sierra
and
Bert
Bates
Chapter 2
Jan, 2009
40
Lectures
www.arian-celina.com
arian.celina@gmail.com
Jan, 2009
41