Objects by Sufian Idris

34
ufian Idris, Jabatan Sains Komputer iversiti Kebangsaan Malaysia Objects

description

Slides on objects by Sufian Idris

Transcript of Objects by Sufian Idris

Page 1: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Objects

Page 2: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• Object is a very important concept in object-orientation.

• When an OO program is executed, it normally creates objects in memory.

• These objects collaborate with each other to perform certain tasks.

• This realizes the runtime behaviour of the program.

Page 3: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

$$$$Kad

A

TM

kuK

ad

AT

Mku

Object Space

Page 4: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Definition of Object

• An object is a thing that has state, behaviour and identity.

• Examples:– The LCD projector in the lecture hall at

FTSM.– Dr Sufian’s Kenari– Dr Sufian’s Waja

Page 5: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object State

• Each object has attributes which collectively represent its state.

BEFORE AFTER

Page 6: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• In Java, the attributes of an object are implemented as variables that belong to that object. These variables are called instance variables.

Example:

Car

“WHH7801”

green

“Sufian Idris”

colour

plateNr

owner

Car

“WHY9531”

blue

“Ali Bakar”

colour

plateNr

owner

A Car object Another Car object

Page 7: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• The atributes of an object normally do not change.

The values of the attributes of an object can change. However, there are attributes with fixed values.

Fridge

“342-0967”

“Ali Bakar”

biru

owner

serialNr

colour

value is fixed

values can be changed

Page 8: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• An object may have links to other objects. These links represent relationships between those objects.

• Example:

: Car

: Person

: Carlinks

The links are added to represent the fact that both cars are owned by the same person.

Page 9: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• Example:

: Car

: Engine

: Chassis

links

The links are added to represent the fact that the engine and the chassis are part of the car. This relationship is also called aggregation.

Page 10: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

In Java, links to objects are represented as object references.

Instance variables can be defined to store such references.

: Computer

datePurch

processor

memory

15032002

: Memory

size 256

: Processor

speed 900

object references

Page 11: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object Behaviour

• Message sending is a mechanism which objects use to interact with other objects.

• To request an object to perform a certain task, an appropriate message needs to be sent to that object.

jump

Page 12: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

An object only responds to messages that it understands.

fly??

Page 13: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object behaviour refers to how an object reacts to messages it receives from its clients.

: Scanner: Temperature

Converter

nextFloat()

The Scanner object responds byinputting a float value and returningthat value to the TemperatureConverterobject.

Page 14: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

: Rectangle

5

colour

width

height 10

green

• An object’s response to a message can cause its state to change.

zoom_2x

10

20

Page 15: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

An object may also send messages to other objects in its response to a message.

switchChannel(5) openCommChannel()switchChannel(5)closeCommChannel()

Page 16: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object Identity

Each object has its own identity which differentiates it from other objects.

Triplets… but each of them has its own identity

Page 17: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

The identity of an object is built-in. There is no need to explicitly define an instance variable to represent the identity of an object.

class Computer {private int computerID;private Date datePurchased;private Processor processor;

…}

Redundant

Page 18: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Relationship between Object State and Object Behaviour

• Object behaviour can affect object state.• Example:

An ATM machine dispensing money causes the amount of money in the machine to decrease.

Page 19: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object state can affect object behaviour.

Number of tins:

20

What if there are no more tins in the machine?

dispense()

Page 20: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Abstraction

• Supposing we need to define an object that represents a cat. What attributes and methods should we define?

Attributes Behaviour

weightlengthheart size

chewdrinkrun

… …

…there are too many!

Page 21: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Humans have the ability to isolate those aspects that are important for some purpose and suppress those aspects that are unimportant.

Page 22: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

A program designer only needs to define the attributes and behaviour that are essential to the problem in hand.

In object-orientation, software objects are abstractions.

: Bird

5

colour

weight

height 10

white

14

0.5

Page 23: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Encapsulation

• Objects should not reveal all of its information. There is certain information that should be hidden.

• Example A restaurant may not want to reveal the specifics of how the dishes it serves are prepared. On the other hand, it needs to reveal the price of each type of dish.

Page 24: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

• Encapsulation is the technique for packaging information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

Page 25: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Information normally hidden by objects:object attributesthe specifics of how they react to the

messages received by them

40

area()

attributes:widthheight…

behaviour:

area()

return width * height;

HIDDEN BY OBJECT

Page 26: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Some benefits of encapsulation:if done properly, modifications to

object implementation should not affect clients

objects can control access to their attributes

Page 27: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object Interface

Not all messages are understood by an object.

To obtain information on the messages which an object understands, we need to refer to its interface.

Page 28: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

The interface of an object reveals public information about the object. It should not contain information hidden by the object.

An example of information that could be in an object’s interface is a list of its public methods; only their signature, not their implementation.

Page 29: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

INTERFACEarea()setWidth(int width)setHeight(int height)

??

yesss!

But where can we find information about an object’s interface?

Rectangleobject

Page 30: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Messages and Methods

• Message sending is a mechanism used by objects to communicate with other objects.

• To get an object to do something, you send an appropriate message to that object.

jump

Page 31: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

A message consists of its name (compulsory) and its parameters (if any).

The receiver object is the object receiving the message. The sender of the message is referred to as the client.

switchChannel(5) openCommChannel()switchChannel(5)closeCommChannel()

clientreceiverobject

Page 32: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

An object implements its behaviour with methods. A method contains implementation details of how an object responds to a certain message.

40

area()

attributes:widthheight…

behaviour:

area()

return width * height;

message

method

Page 33: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object Collaboration

• Isolated objects are useless in object-oriented systems.

“No object is an island”

Page 34: Objects by Sufian Idris

©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia

Object cooperation is very important in object-oriented software. In general, the behaviour of an OO software is realized through object collaboration.