objects by sufian idris
DESCRIPTION
Slides on objects by Sufian IdrisTRANSCRIPT
©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia
Objects
©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.
©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia
$$$$Kad
A
TM
kuK
ad
AT
Mku
Object Space
©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
©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia
Object State
• Each object has attributes which collectively represent its state.
BEFORE AFTER
©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
©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
©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.
©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.
©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
©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
©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia
An object only responds to messages that it understands.
fly??
©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.
©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
©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()
©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
©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
©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.
©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()
©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!
©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.
©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
©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.
©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.
©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
©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
©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.
©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.
©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
©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
©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
©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
©Sufian Idris, Jabatan Sains KomputerUniversiti Kebangsaan Malaysia
Object Collaboration
• Isolated objects are useless in object-oriented systems.
“No object is an island”
©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.