Object, Class, Message & Methods
OOP
Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare share some similar information like a name, an address, & a budget.
Beside attribute, object also have some method. Ex: Benjamin & Bernie as customers have some behaviour like making purchase.
Objects
Benjamin as an object:attributes:
name=“Benjamin”address=“1, Robinson road”budget=“2000”
methods:purchase() {send a purchase
request}getBudget() {return budget}
Object Structure
Bernie as an object:attributes:
name=“Bernie”address=“18, Sophia road”budget=“1000”
methods:purchase() {send a purchase
request}getBudget() {return budget}
Object Structure
A Class is a definition template for structuring and creating objects with the same attributes and methods.
Ex: Like Benjamin & Bernie, All customers of HomeCare share the same set of attribute & method definition. They all have attributes: name, address & budget, and methods purchase() & getBudget().
Class
Class Customer:attributes:
nameaddressbudget
methods:purchase() {send a purchase
request}getBudget() {return budget}
Class
One major difference between object & class is in the way are treated in objects & class.
A Class attributes & methods are declarations that do not contain values.
An Object are created instances of a class that has it own attributes & methods (state).
Object vs class
Examine : Object & Class at salesperson
Object & Class
Sean as an object:attributes:
name=“Sean”methods:
takeOrder() {check with the warehouse on stock
abailabilitycheck with the warehouse on delivery
scheduleif ok then{instruct warehouse to deliver
stock(address, date) return okelse return not ok}
Object Structure
Class Salesperson:attributes:
namemethods:
takeOrder() {check with the warehouse on stock
abailabilitycheck with the warehouse on delivery
scheduleif ok then{instruct warehouse to deliver
stock(address, date) return okelse return not ok}
Class
Objects communicate with one another by sending messages.
A messages is a method call from a message-sending object (sender) to a message-receiving object (receiver).
An object responds to message by executing one of its methods.
Such parameter allows for added flexibility in message passing.
Offcourse, an object may have as many methods as required.
Message & Method
A message composed of:◦ An object identifier. ◦ A method name.◦ arguments (parameter).
Ex: Benjamin sent a messages to Sean when Benjamin wanted to buy a sofa set. The reasonable location is in Benjamin’s purchase() method
Message Components
Benjamin as an object:attributes:
name=“Benjamin”address=“1, Robinson road”budget=“2000”
methods:purchase(
Sean.takeOrder(“Benjamin”, “Sofa”,”1, Robinson Road”, “12 September 2012”)
getBudget() {return budget}
Message components
Sean is receiver of the message takeOrder is
method call on Sean
Reds are arguments of
message
A message is valid if the receiver has a method that corresponds to the method named in message & the appropriate arguments.
Ex: the takeOrder() message is valid because Sean has a corresponing method & the required arguments.
Method
Sean as an object:attributes:name=“Sean”methods:takeOrder(who,stock,address,date) {check with the warehouse on stock abailabilitycheck with the warehouse on delivery scheduleif ok then{instruct warehouse to deliver stock to address on date return okelse return not ok}
Object Structure
Objects are created from classess. Created object instances are individuals
with their own state. Ex: Counter
Creating Objects
First Counter ObjectAttributes:
number = 10Methods:
add() {number = number + 1}initialize() {number = 0}getNumber() {return number}
First Object
Second Counter ObjectAttributes:
number = 2Methods:
add() {number = number + 1}initialize() {number = 0}getNumber() {return number}
Second Object
Third Counter ObjectAttributes:
number = 7Methods:
add() {number = number + 1}initialize() {number = 0}getNumber() {return number}
Third Objek
Class CounterAttributes:
numberMethods:
add() {number = number + 1}initialize() {number = 0}getNumber() {return number}
Class Counter
Objects are defined by classes. Objects from the same class share the same definition of
attributes and methods. Objects from the same class may not have the same attribute
values. Objects from different classes do not share the same definition of
attributes or methods. Objects created from the same class share the same definition of
attributes and methods but their state may differ. A method is a set of operations executed by an object upon the
receipt of a message. A message has three components: an object identifier, a method
name and arguments. A message-receiving object is a server(receiver)to a message-
sending object known as a client(sender).
Summary