Lecture#01, object orientation

Post on 17-Nov-2014

451 views 2 download

Tags:

description

 

Transcript of Lecture#01, object orientation

By: Altaf Hussain

SS KRLBS(CS), AU Peshawar, MS(CSE), NUST Islamabad

Understand the basic principles of object orientation

Understand the basic concepts and terms of object

Basic Principles of Object Orientation Basic Concepts of Object Orientation

Object Orientation

Enca

psul

atio

n

Abs

trac

tion

Hie

rarc

hy

Mod

ular

ity

5

Abstraction allows us to manage complexity by concentrating on essential aspects making an entity different from others.

A process allowing to focus on most important aspects while ignoring less important details.

Salesperson

Not saying Which

salesperson – just a

salesperson in general!!!

CustomerProduct

Manages Complexity

Hide implementation from clients• Clients depend on interface

How does an object encapsulate?What does it encapsulate?

The breaking up of something complex into manageable pieces

Order ProcessingSystem

Order Entry

Order Fulfillment

Billing

Decreasing abstraction

Increasing abstraction

Asset

RealEstate

Savings

BankAccount

Checking Stock

Security

Bond

Elements at the same level of the hierarchy should be at the same level of abstraction

Levels of abstraction – ordering of abstraction in a tree like structure

Basic Principles of Object Orientation Basic Concepts of Object Orientation

Object Class Attribute Operation Interface Implementation Association Aggregation Composition Generalization Super-Class Sub-Class Abstract Class Concrete Class Discriminator Polymorphism

Truck

Chemical Process

Linked List

Informally, an object represents an entity, either physical, conceptual, or software

• Physical entity

• Conceptual entity

• Software entity

An object is a concept, abstraction, or thing with sharp boundaries and meaning for an application

An object is something that has:• State• Behavior• Identity

14

Object-oriented programming supports the view that programs are composed of objects that interact with one another.

How would you describe an object? Using its characteristics (has a ----?) and its

behaviors (can do ----?) Object must have unique identity (name) :

Basketball, Blue ball Consider a ball:

• Color and diameter are characteristics (Data Declarations)

• throw, bounce, roll are behaviors (Methods)

15

A class defines the general nature of a collection of objects of the same type.

The process creating an object from a class is called instantiation.

Every object is an instance of a particular class. There can be many instances of objects from the

same class possible with different values for data.

Objects Example

16

class Rose

blueRose

redRose

class

objectsObject References

OO Principle: Abstraction

A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics• An object is an instance of a class

A class is an abstraction in that it:• Emphasizes relevant characteristics• Suppresses other characteristics

a + b = 10

ClassCourse

PropertiesName

LocationDays offeredCredit hours

Start timeEnd time

BehaviorAdd a student

Delete a studentGet course roster

Determine if it is full

Professor

nameempID

create( )save( )delete( )change( )

Class Name

Attributes

Operations

A class is comprised of three sections• The first section contains the class name• The second section shows the structure (attributes)• The third section shows the behavior (operations)

How many classes do you see?

Objects Class

Professor Smith

Professor Jones

Professor Mellon

Professor

A class is an abstract definition of an object• It defines the structure and behavior of each object in the

class• It serves as a template for creating objects

Objects are grouped into classes

:CourseOffering

number = 101startTime = 900endTime = 1100

:CourseOffering

number = 104startTime = 1300endTime = 1500

CourseOffering

numberstartTime endTime

Class

Attribute

Object

Attribute Value

CourseOffering

addStudent deleteStudentgetStartTimegetEndTime

Class

Operation

24

A class that lacks a complete implementation and provides operations without implementing some methods

Cannot be used to create objects; cannot be instantiated

A concrete sub-class must provide methods for unimplemented operations

25

Has methods for all operations Can be instantiated Methods may be:

a) defined in the class orb) inherited from a super-class

26

Discriminator – an attribute that defines sub-classes Example: “status” of company staff is a possible

discriminator to derive “management”, “senior” and “junior” sub-classes.

27

Ability to dynamically choose the method for an operation at run-time or service-time

facilitated by encapsulation and generalization:• encapsulation – separation of interface from

implementation• generalization –organizing information such

that the shared features reside in one class and unique features in another

Operations could be defined and implemented in the super-class, but re-implemented methods are in unique sub-classes.

Manufacturer AManufacturer B Manufacturer C

OO Principle:Encapsulation

The ability to hide many different implementations behind a single interface

Tube

Pyramid

Cube

Shape

DrawMoveScaleRotate

<<interface>>

Realization relationship (stay tuned for realization relationships)

Interfaces formalize polymorphism Interfaces support “plug-and-play” architectures

Association• Aggregation• Composition

Dependency Generalization Realization

Professor UniversityWorks for

Class

Association

Association Name

Professor University

EmployerEmployee

Role Names

Models a semantic connection among classes

Student Schedule

Whole

Aggregation

Part

A special form of association that models a whole-part relationship between an aggregate (the whole) and its parts

Student Schedule

Whole

Aggregation

Part

A form of aggregation with strong ownership and coincident lifetimes• The parts cannot survive the whole/aggregate a

Multiplicity defines how many objects participate in a relationships• The number of instances of one class related to

ONE instance of the other class• Specified for each end of the association

Associations and aggregations are bi-directional by default, but it is often desirable to restrict navigation to one direction• If navigation is restricted, an arrowhead is

added to indicate the direction of the navigation

Student Schedule1 0..*

Multiplicity

Navigation

Client Supplier

Package

ClientPackage SupplierPackage

Client Supplier

Class

Dependency relationship

Dependency relationship

Component

A relationship between two model elements where a change in one may cause a change in the other

Non-structural, “using” relationship

A relationship among classes where one class shares the structure and/or behavior of one or more classes

Defines a hierarchy of abstractions in which a subclass inherits from one or more super-classes• Single inheritance• Multiple inheritance

Generalization is an “is-a-kind of” relationship

Accountbalancenamenumber

Withdraw()CreateStatement()

Checking

Withdraw()

Savings

GetInterest()Withdraw()

Superclass (parent)

Subclasses

Generalization Relationship

Ancestor

Descendents

One class inherits from another

Airplane Helicopter Wolf Horse

FlyingThing Animal

Bird

multipleinheritance

Use multiple inheritance only when needed, and always with caution !

A class can inherit from several other classes

Inheritance leverages the similarities among classes

A subclass inherits its parent’s attributes, operations, and relationships

A subclass may:• Add additional attributes, operations,

relationships• Redefine inherited operations (use caution!)

Common attributes, operations, and/or relationships are shown at the highest applicable level in the hierarchy

Truck

tonnage

GroundVehicle

weight

licenseNumber

Car

owner

register( )

getTax( )

Person

0..*

Trailer

1Superclass

(parent)

Subclass

generalization

size

Component

Interface

Use Case Use-Case Realization

Elided form

Class

Interface

Subsystem

Interface

Canonical form

One classifier serves as the contract that the other classifier agrees to carry out

Found between:• Interfaces and the classifiers that realize them• Use cases and the collaborations that realize them