Академический Документы
Профессиональный Документы
Культура Документы
Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface
Basic Components
a software bundle of related states (properties, or variables) and behavior (method)
State is stored in fields (variables in some programming languages) Method exposes objects behavior.
Show
the
.me
Set
the
.me
Proper.es
Methods
Time
Silence
the
alarm
Alarm
.me
Sound
the
alarm
Set
the
alarm
Enable
the
alarm
3 4
Basic Components
Class: Blueprint from which objects are created
Multiple Instances created from a class
Basic Components
Package: a namespace for organizing classes and interfaces
8/28/12
Outline
Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface
Data Encapsulation
An ability of an object to be a container (or capsule) for related properties and methods.
Preventing unexpected change or reuse of the content
Data hiding
Object can shield variables from external access.
Private variables Public accessor and mutator methods
Data Encapsulation
public class Clock! {! !private long time, alarm_time;! !private String serialNo;! !public void setTime(long _time){! !time = _time;! !}! !public void setAlarmTime(long_time){! !! !alarm_time = _time;! !}! !public long getTime(){return time}! !public long getAlarmTime(){return alarm_time}! public void noticeAlarm(){ ring alarm } ! !protected void set serialNo(String _serialNo){}! } !
9
Outline
Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface
10
Inheritance
The ability of a class to derive properties from a previously defined class. Relationship among classes. Enables to reuse software components
E.g. java.lang.Object() toString(), notifyAll(), equals(), etc.
Example: Inheritance
clock
Sports
Watch
11
Radio
Clock
12
8/28/12
Overriding Methods
public class RadioClock! {! !@override! public void noticeAlarm(){! ring alarm ! turn_on_the_Radio! } !! } !
14
Outline
Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface
16
Polymorphism
Having multiple forms Ability to create a variable, or an object that has more than one form.
Polymorphic method
RadioClock myRadioClock = new RadioClock();! Clock myClock = myRadioClock;! myClock.notifyAlarm();!
17
18
8/28/12
Dynamic Binding
myClock actually references an instance of RadioClock
It will turn on the radio.
Outline
Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface
The version of a method notifyAlarm() is decided at execution time. (not at compilation time)
19
20
Abstract
A special kind of class that cannot be instantiated. It allows only other classes to inherit from it. It enforces certain hierarchies for all the subclasses
Interface
An Interface is NOT a class. An Interface has NO implementation inside.
Definitions of methods without body.
21
22
Comparison-1
Feature
Mul2ple
inheritance
Default
implementa2on
Interface
Abstract
Class
Feature
Adding
func2onality
(
Versioning)
A
class
may
inherit
Only
one
several
interfaces
Cannot
provide
any
Can
provide
code
complete,
default
code
and/or
just
the
details
that
have
to
be
overridden.
Cannot
have
access
Can
have
it.
modiers.
(everything
is
assumed
as
public)
23
Comparison-2
Interface
For
a
new
method,
we
have
to
track
down
all
the
implementa2ons
of
the
interface
and
dene
implementa.on
for
the
new
method
No
elds
can
be
dened
in
interfaces
Abstract
Class
For
a
new
method,
we
can
provide
default
implementa2on
and
all
the
exis.ng
code
might
work
properly.
Fields
and
constants
can
be
dened
24
Access Modier
8/28/12
Next Reading
3.2 from Rosen Chapter 10 from Prichard
25