+ All Categories
Home > Documents > OOP. Object attribute definition allows for object to have independent attribute values. Ex:...

OOP. Object attribute definition allows for object to have independent attribute values. Ex:...

Date post: 05-Jan-2016
Category:
Upload: shawn-manning
View: 215 times
Download: 2 times
Share this document with a friend
21
Object, Class, Message & Methods OOP
Transcript
Page 1: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Object, Class, Message & Methods

OOP

Page 2: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 3: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Benjamin as an object:attributes:

name=“Benjamin”address=“1, Robinson road”budget=“2000”

methods:purchase() {send a purchase

request}getBudget() {return budget}

Object Structure

Page 4: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Bernie as an object:attributes:

name=“Bernie”address=“18, Sophia road”budget=“1000”

methods:purchase() {send a purchase

request}getBudget() {return budget}

Object Structure

Page 5: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 6: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Class Customer:attributes:

nameaddressbudget

methods:purchase() {send a purchase

request}getBudget() {return budget}

Class

Page 7: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 8: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Examine : Object & Class at salesperson

Object & Class

Page 9: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 10: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 11: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 12: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 13: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 14: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 15: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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

Page 16: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Objects are created from classess. Created object instances are individuals

with their own state. Ex: Counter

Creating Objects

Page 17: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

First Counter ObjectAttributes:

number = 10Methods:

add() {number = number + 1}initialize() {number = 0}getNumber() {return number}

First Object

Page 18: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Second Counter ObjectAttributes:

number = 2Methods:

add() {number = number + 1}initialize() {number = 0}getNumber() {return number}

Second Object

Page 19: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Third Counter ObjectAttributes:

number = 7Methods:

add() {number = number + 1}initialize() {number = 0}getNumber() {return number}

Third Objek

Page 20: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

Class CounterAttributes:

numberMethods:

add() {number = number + 1}initialize() {number = 0}getNumber() {return number}

Class Counter

Page 21: OOP.  Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare  share some.

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


Recommended