Object-Oriented Programming Using Java

processroguishΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

56 εμφανίσεις

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

1

Object
-
Oriented Programming





Using Java

Introduction to OOP

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

2

Programming Paradigms


Imperative Programming
(
C
, Pascal, …)



Functional Programming
(Lisp, ML, …)



Logic Programming
(Prolog)



Concurrent Programming
(Ada, Occam, …)



Object
-
oriented Programming

(Smalltalk, C++,
Java
, …)


This is what we shall teach (through Java).

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

3

The Object
-
Oriented (OO)
Programming Paradigm


Object
-
oriented Programming

is one of the
programming
paradigms

(school of thought,
methodology) in computer science.



Object
-
oriented Programming is well
-
known in
the business world by the name of


Object Technology
.’



Synonyms:

Object

==
Instance

==

Entity




Class

~=

Static

~=

Type

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

4

Why OOP?


Objects have two properties


fields

(
instance

variables) and
methods
.


Fields tell you what an object
is

(properties).


Methods tell you what an object
does
(tasks).



Object oriented programming is alleged to have
a number of advantages including:


Simpler, easier to read programs


More efficient reuse of code


Faster time to market


More robust, error
-
free code

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

5

Customer

bill

Customer

michael

Account

peterSaving

Objects


Our world is full of
objects
.




Graphical representation of objects

Object ‘type’

i.e. Class name

Object name

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

6


Our world is full of
objects
.




Graphical representation of objects

Customer

bill

Objects

Customer

michael

Account

peterSaving

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

7

Modeling Our World


We try to model this object world.



Objects can
accomplish tasks

and
keep data/state
.


e.g.

A drink dispensing machine
sells Coke
.


It has a
stock

of
100 cans
.



Inhuman?!


Certainly, but it helps us to program a computer in an
organized and manageable manner.

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

8

Verbs and Nouns


Verbs


Action/ Task


Method



Nouns


Entity


Field


A drink dispenser can
sell coke
,
sell lemon tea
.
Its stock includes
number of coke
,
number of
lemon tea
. It can
deliver change
.



Any implicit (or missing) verb/ noun?


Can you suggest a class name?

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

9

Classes


A
class

(
e.g.
, Customer) is a kind of mold or
template to create objects
(e.g
., michael and bill).


An object is an
instance of

a class.

The object
belongs to

that class.

Customer

bill

Customer

michael

Customer


instance
-
of’

Class

Object

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

10

More Class Examples



Person

michael

Person

billGates

Person

Account

johnCheque

Account

peterSaving

Account

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

11

Object
-
Oriented Programming


We first
define

classes
.



While the program is running, we may
create

objects
from these classes.



We may
store information

in classes and objects.



We
send
messages

to a class or an object to instruct it to
perform a task.


(For example, we send a
deposit $250.00

message to an
Account object to deposit $250.00 into the account.)

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

12

Account

new

Creating Objects


An object is created by sending a
new

message to a class.

Method

Message sent to Class Account:
new

(ask it to create a new Account object)

Message sender

An instance (new object) is
returned to the sender

main( )


Account

johnSaving


instance
-
of’

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

13

Messages and Methods


An object consists of
fields

to store data

and
methods

to manipulate the data
.

Account

Account

mySavingAC

name

Michael

deposit

withdraw



balance

$123.45

Fields

Methods

Message:

deposit $250.00

Argument of the
message

$250.00

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

14

Messages and Methods


A message may instruct an object to change the
fields (state) of the object
itself

through a method.

Account

myAccount

Account

name

Michael

deposit

withdraw



balance

$123.45

yourAccount

Account

name

Raymond

deposit

withdraw



balance

$58.12

$250.00

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

15

Messages and Methods


A message may instruct an object to change the
fields (state) of the object
itself

through a method.

Account

myAccount

Account

name

Michael

deposit

withdraw



balance

$373.45

yourAccount

Account

name

Raymond

deposit

withdraw



balance

$58.12

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

16

Messages and Methods


A method may
return

a value

to the message
sender.

Account

myAccount

Account

name

Michael

deposit

withdraw



balance

$373.45

$999.00

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

17

Messages and Methods


An object may
return

a value to the message
sender through the method.

Account

myAccount

Account

name

Michael

deposit

withdraw



balance

$373.45

Result


Failure


2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

18

Messages and Methods


Of course, we CANNOT send
arbitrary

messages
to an
arbitrary

classes or objects.

Account

myAccount

Account

name

Michael

deposit

withdraw



balance

$373.45

rob $10,000.00

sell Coke

deposit $30.00

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

19

Summary


Object
-
Oriented Programming (OOP)
treats entities as objects,

modeled by classes.



Objects and classes can


store data in fields and


perform actions in methods.

2008
-
2009 1c

Michael Fung, CS&E, The Chinese University of HK

20

End Note


Readings and References


Preface


Chapter 1 Introduction



Exercise


1.1



Programming Projects


1.1, 1.2