You are on page 1of 37

CSE/IT 213

Object-oriented Programming (OOP)


Classes and Objects
UML
Inheritance
Composition

Attributes (data) and behaviors (methods =


functions) are normally separated
Code is placed into completely distinct
functions/procedures/subroutines
Black boxes: inputs go in, outputs come out
Data is placed in separate structures
Data is manipulated by functions/
procedures/subroutines

Encapsulation: attributes and behaviors are


contained within a single entity (object)
Objects are responsible for manipulating their
own data
Data hiding: access to certain attributes and
methods can be hidden from other objects
One object calls a method of (sends a
message to) another object that returns a
result
The caller doesnt care how that data was
obtained

Procedural programming

The data of a system is normally separated from the


operations that manipulate the data.

Object-oriented programming

The data and the operations that manipulate the


data are both encapsulated in the object.

The building blocks of object-oriented


programming
Attributes: data representing the state of the
object
Differentiates between various objects
Instance fields

Behaviors: methods used to perform operations


on data
What an object does
Invoked by sending a message to it
Specialized behaviors include setting and getting data

An employee is an object

Attributes (data): Social Security number, date of


birth, gender, phone number, address, email
address, etc.
Behaviors (methods): working, getting paid,
vacation, bonus, etc.

Object

An entity that contains both attributes and


behavior.

Getters and setters


support data hiding

Objects should not directly manipulate data within


another object (encapsulation)
Provide controlled access to an objects data

private String name;!


public void setName(String n) {!
name = n;!
}!
public String getName() {!
!return name;!
}!

The interface of a method is all a user needs to


know
The name of the method
The parameters passed to the method
The return type of the method

For example

A Payroll object must get the Social Security number for


each employee
Can send a message to each Employee object using its
getSocialSecurityNumber() method
Each Employee object receives the message and returns
the requested data

public class Employee {!


!public String getSocialSecurityNumber();!
!private String SocialSecurityNumber;!
}!
public class Payroll {!
!public Payroll(Employee employee);!
!public void payTaxes() {!
! !employee.getSocialSecurityNumber()!
! !}!
!}!
}!

Classes are blueprints, or templates, for making


objects
Objects are instances of classes
Each object has its own copy of attributes and
methods
Example: Two Employee objects

John & Mary


John and Mary have different attributes (e.g. SSN,
birthday, etc)

Classes can be thought of as abstract data


types
Define all attributes and behaviors that all
objects created with this class will possess
You must design a class before you create an
object

public class Person { !


!// Attributes !
!private String name; private String address; !
// Methods !
!public String getName() { return name; !
!}!
!public void setName(String n) { !
!
!name = n;!
!}
!!
!public String getAddress () { !
!
!return address; !
!}!
!public void setAddress(String adr) { !
!
!address = adr; !
!}!
} !

Attributes

Each class must define the attributes that will store


the state of each object instantiated from that class

Methods

Every object instantiated from a class has the


methods defined by that classes
May provide fundamental, internal behavior of class
May implement behaviors called from other objects
via messages

Communication mechanism between objects


To instruct a class or an object to perform a task, we
send a message to it.
You can send a message only to the classes and
objects that understand the message you sent to
them.
A class or an object must possess a matching
method to be able to handle the received message.
A method defined for a class is called a class
method, and a method defined for an object is called
an instance method.
A value we pass to an object when sending a
message is called an argument of the message.
Access designations: public and private

Chapter 1

- 15

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

Unified Modeling
Language (UML)
Object name: Payroll
Data (attributes): pay
Behaviors (methods)

calculatePay() Includes
return data types after :

Access designation
+ public - private

Method parameters (in


parens)

<Class Name>

Example:

Account

We use a rectangle to
represent a class with
its name appearing
inside the rectangle.

Motorcycle

The notation we used here is based on the industry standard notation


called UML, which stands for Unified Modeling Language.

Chapter 1

- 17

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

<Object Name>

We use a rectangle to
represent an object and
place the underlined
name of the object
inside the rectangle.

Example:

SV198

This is an object named


SV198.

Chapter 1

- 18

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

<Object Name> : <Class Name>

This notation indicates


the class which the
object is an instance.

Example:

SV198 : BankAccount

This tells an object


SV198 is an instance of
the BankAccount class.

Chapter 1

- 19

The McGraw-Hill
Companies, Inc. Permission
required for reproduction
or display.

Message deposit with


the argument 250.00 is
sent to a BankAccount
object SV198.

deposit 250.00
SV198 : BankAccount

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
120

Ask for the current


balance of this
particular account.

getCurrentBalance()
SV198 : BankAccount

current balance

The current balance of


SV198 is returned.
The McGraw-Hill Companies, Inc.
Permission required for
reproduction or display.

Ch
ap
ter
121

Ask for the maximum


possible speed for all
MobileRobot objects is
returned.

MobileRobot
getMaximumSpeed()

maximum speed

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
122

SV129 : BankAccount

SV098 : BankAccount

SV211 : BankAccount

current balance
908.55

current balance
1304.98

current balance

All three BankAccount


objects possess the
same instance data
value current balance.

354.00

The actual dollar


amounts are, of course,
different.

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
123

BankAccount
minimum balance
100.00

There is one copy of


minimum balance for
the whole class and
shared by all instances.

This line is an
instance-of
relationship.

SV129 : BankAccount

SV098 : BankAccount

SV211 : BankAccount

current balance
908.55

current balance
1304.98

current balance
354.00

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
124

SV129 : BankAccount
minimum balance
100.00
current balance
908.55

When the class icon is


not shown, we include
the class data value in
the object icon itself.

The McGraw-Hill Companies, Inc.


Permission required for
reproduction or display.

Ch
ap
ter
125

Encapsulation

The internal representation, the attributes and behaviors, of


an object is hidden from all other objects. Only the details
pertinent to its use (i.e., interfaces) are accessible to other
objects.

Class interfaces define the fundamental means of


communication between objects
Each class design specifies interfaces for instantiation
(e.g., constructors) and operation (e.g., public
methods) of objects
Any behavior of an object is invoked by a message
sent using a provided interface

Implementation

Only public attributes and methods are considered


the interface

User should not see any part of the


implementation
Implementation can change and will not
affect users code

public class IntSquare { !


!// private attribute !
!private int squareValue;!
// public interface !
!public int getSquare (int value) { !
! !SquareValue = calculateSquare(value); !
! !return squareValue; !
!} !
// private implementation !
!private int calculateSquare (int value) { !
! !return value * value; !
!} !
} !

The abstraction of common attributes and


behaviors allows the creation of a brand new
class that inherits the attributes and methods
of another class.
Common attributes and behaviors can be
moved up to a parent class (or superclass)

Only need to add specialized attributes and


behaviors to child class (or subclass)
Objects contain all attributes and behaviors from
own class and all ancestors

Superclass

Contains all attributes and behaviors common to


classes that inherit from it
Also known as a parent class or generalization

Subclass
A specialization of the superclass with unique
attributes and behaviors
Inherits all attributes and behaviors from the
superclass
Is-a relationships

A subclass is-a superclass.

Are they related?

A dog is-a mammal


A cat is-a mammal

The power of inheritance lies in abstraction


and organization

Single-inheritance: one parent class, but many child


classes (e.g., Java)
Multiple-inheritance: multiple parent classes and
child classes (e.g., C++)

Each subclass may respond differently to the


same message

We can create new objects by putting


together other objects

Car contains an engine, steering wheel, tires, etc.

Abstract out common attributes for


inheritance
Same can be done for composition
Example, car has an engine. We want to keep
the car and engine separate in case we want
to put a new engine without changing the car

Has-a relationships
A class has-a collection of other class.
Since composition items do not inherit from
one another, the type of relationship is a
has-a relationship
Example: Car has-a engine, but a car is not
an engine

You might also like