Lecture#01, object orientation

42
By: Altaf Hussain SS KRL BS(CS), AU Peshawar, MS(CSE), NUST Islamabad

description

 

Transcript of Lecture#01, object orientation

Page 1: Lecture#01, object orientation

By: Altaf Hussain

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

Page 2: Lecture#01, object orientation

Understand the basic principles of object orientation

Understand the basic concepts and terms of object

Page 3: Lecture#01, object orientation

Basic Principles of Object Orientation Basic Concepts of Object Orientation

Page 4: Lecture#01, object orientation

Object Orientation

Enca

psul

atio

n

Abs

trac

tion

Hie

rarc

hy

Mod

ular

ity

Page 5: Lecture#01, object orientation

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.

Page 6: Lecture#01, object orientation

Salesperson

Not saying Which

salesperson – just a

salesperson in general!!!

CustomerProduct

Manages Complexity

Page 7: Lecture#01, object orientation

Hide implementation from clients• Clients depend on interface

How does an object encapsulate?What does it encapsulate?

Page 8: Lecture#01, object orientation

The breaking up of something complex into manageable pieces

Order ProcessingSystem

Order Entry

Order Fulfillment

Billing

Page 9: Lecture#01, object orientation

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

Page 10: Lecture#01, object orientation

Basic Principles of Object Orientation Basic Concepts of Object Orientation

Page 11: Lecture#01, object orientation

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

Page 12: Lecture#01, object orientation

Truck

Chemical Process

Linked List

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

• Physical entity

• Conceptual entity

• Software entity

Page 13: Lecture#01, object orientation

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

Page 14: Lecture#01, object orientation

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)

Page 15: Lecture#01, object orientation

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.

Page 16: Lecture#01, object orientation

Objects Example

16

class Rose

blueRose

redRose

class

objectsObject References

Page 17: Lecture#01, object orientation

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

Page 18: Lecture#01, object orientation

a + b = 10

ClassCourse

PropertiesName

LocationDays offeredCredit hours

Start timeEnd time

BehaviorAdd a student

Delete a studentGet course roster

Determine if it is full

Page 19: Lecture#01, object orientation

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)

Page 20: Lecture#01, object orientation

How many classes do you see?

Page 21: Lecture#01, object orientation

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

Page 22: Lecture#01, object orientation

:CourseOffering

number = 101startTime = 900endTime = 1100

:CourseOffering

number = 104startTime = 1300endTime = 1500

CourseOffering

numberstartTime endTime

Class

Attribute

Object

Attribute Value

Page 23: Lecture#01, object orientation

CourseOffering

addStudent deleteStudentgetStartTimegetEndTime

Class

Operation

Page 24: Lecture#01, object orientation

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

Page 25: Lecture#01, object orientation

25

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

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

Page 26: Lecture#01, object orientation

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.

Page 27: Lecture#01, object orientation

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.

Page 28: Lecture#01, object orientation

Manufacturer AManufacturer B Manufacturer C

OO Principle:Encapsulation

The ability to hide many different implementations behind a single interface

Page 29: Lecture#01, object orientation

Tube

Pyramid

Cube

Shape

DrawMoveScaleRotate

<<interface>>

Realization relationship (stay tuned for realization relationships)

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

Page 30: Lecture#01, object orientation

Association• Aggregation• Composition

Dependency Generalization Realization

Page 31: Lecture#01, object orientation

Professor UniversityWorks for

Class

Association

Association Name

Professor University

EmployerEmployee

Role Names

Models a semantic connection among classes

Page 32: Lecture#01, object orientation

Student Schedule

Whole

Aggregation

Part

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

Page 33: Lecture#01, object orientation

Student Schedule

Whole

Aggregation

Part

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

Page 34: Lecture#01, object orientation

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

Page 35: Lecture#01, object orientation

Student Schedule1 0..*

Multiplicity

Navigation

Page 36: Lecture#01, object orientation

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

Page 37: Lecture#01, object orientation

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

Page 38: Lecture#01, object orientation

Accountbalancenamenumber

Withdraw()CreateStatement()

Checking

Withdraw()

Savings

GetInterest()Withdraw()

Superclass (parent)

Subclasses

Generalization Relationship

Ancestor

Descendents

One class inherits from another

Page 39: Lecture#01, object orientation

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

Page 40: Lecture#01, object orientation

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

Page 41: Lecture#01, object orientation

Truck

tonnage

GroundVehicle

weight

licenseNumber

Car

owner

register( )

getTax( )

Person

0..*

Trailer

1Superclass

(parent)

Subclass

generalization

size

Page 42: Lecture#01, object orientation

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