1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

26
1 A Student Guide to A Student Guide to Object- Orientated Object- Orientated Systems Systems Chapter 4 Objects and Chapter 4 Objects and Classes: the basic Classes: the basic concepts concepts

Transcript of 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

Page 1: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

1

A Student Guide to Object- A Student Guide to Object- Orientated SystemsOrientated Systems

Chapter 4 Objects and Classes: the Chapter 4 Objects and Classes: the basic conceptsbasic concepts

Page 2: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

2

Objects and Classes – the basic conceptsObjects and Classes – the basic concepts

Use cases model the user’s view of the functionality of a system. Each use case represents a task or major chunk of functionality

Object orientated software structure based on objects

Objects must deliver all the system functionality

Page 3: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

3

What is an object?What is an object?

The most important concept in object-orientated development

A representation of something in the application domain about which we need to store data to enable the system to provide the required functionality.

Page 4: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

4

What is an object?What is an object?

aBike :Bike

type = men’sdailyHireRate = £8deposit = £50

Object name – two optional parts, both underlined

aBike = this specific object’s name

Bike = the class the object belongs to

The object’s attribute values

Attributes are data items defined as part of an object or class.

• type is an attribute• men’s is the value of the ‘type’ attribute for this object

2 sections, top is the object’s name, bottom is the object’s attribute values

Page 5: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

5

More about objectsMore about objects Every object belongs to a class

Objects in an O-O system can represent

– physical things (customers, products, members, and

books), – conceptual things (orders, loans, reservations

and cancellations) – organizational things (companies or

departments). – computer implementation features (GUI

windows, files or linked lists)

Page 6: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

6

More about objectsMore about objectsEvery object in a system has three

characteristics– Behaviour, real world objects

have certain attributes and behaviour

– State, object behaviour may vary depending on its state

– Identity, each object has a unique identity and existence

Page 7: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

7

Object characteristics – behaviourObject characteristics – behaviour

Real world objects Car has data and characteristic behaviour.

Data – amount of fuel, engine temperature, speed. Behaviour – stop, start, move.

Object orientated objects In OO data becomes attributes Behaviour becomes operations (procedures) Data and behaviour are packaged together

Page 8: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

8

Object characteristics – stateObject characteristics – state

An object’s state is determined by the value of its attributes

e.g. Bank a/c with no overdraft allowed

Attribute determining state = balance

Value of balance attribute = £100Behaviour – Withdrawal of £90 allowedWithdrawal of £150 denied

Value of balance attribute = £300Behaviour –

Page 9: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

9

Object characteristics – identityObject characteristics – identity

Every object has an identityEvery object is a uniqueEach object has a separate existence

and ultimately a separate space in memory

Objects whose attribute values are identical are totally distinct from one another.

Page 10: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

Identification Of ObjectsIdentification Of Objects

Many methodologists have their own favorite approaches

All techniques have short comingsfail to identify all objectsidentifies false objects

Page 11: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

Using The Things To Be ModeledUsing The Things To Be Modeled

Basic Steps Are:– Identify individual or group things, such as

persons, roles, organizations, logs, reports, forms, etc. in the application domain

– Identify the corresponding objects and classesRequires significant experience with OO to

apply successfully

Page 12: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

Using NounsUsing Nouns

This assumes that written documents about the domain exist

Steps are:– Obtain or author a narrative English text that represents

an informal description of the problem to be solved.– Use the nouns, pronouns, and noun phrases to identify

objects and classes– Verbs and predicate phrases are used to identify

services

Page 13: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

13

Encapsulation - packaging related data and operations together

Data hiding – making the internal details of an object inaccessible to another object

Public interface – provides the services an object makes available to other objects.

EncapsulationEncapsulation

Page 14: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

14

EncapsulationEncapsulation

dataOperations

Data inside an object is surrounded by a protective ring of operationsThe data is protected by the operations that encapsulate it.

Public interface – an object’s name, class and operations, the only parts of the object accessible to other objects.

Page 15: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

15

ClassClass

An object is defined in terms of its class

A class of objects is a group of objects with the same set of attributes, relationships and behaviour

An object is an instance of a class

Instantiation is creating a new object

Page 16: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

16

ClassClassBike

bike#availabletypesizemakemodeldailyHireRatedeposit

getCharges (no.days)findBike (bike#)

registerBike (bikeDetails)

getBike# ()

Class name – starts with capital letter, if two words ‘CarPark’

Attributes – lower case, no spaces, each subsequent word starts with capital letter

Operations – same notation as attributes

Page 17: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

17

RelationshipsRelationships

Three types of relationships

• Association

• Aggregation

• Generalization (Inheritance)

• Dependency

These are links between classes that are used for message passing

A navigable path between objects

Page 18: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

18

Association – a link between two Association – a link between two classes indicating a relationshipclasses indicating a relationship

0..*

Student

1..*

Course

0..* 1..*

studies

Association relationship between Student and Course classes

• The association can be named: a student studies a course. • An association has two ends each attached to a class.• The numbers and asterisk on the line indicate the multiplicity of the association.

Page 19: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

19

Multiplicity – UML notationMultiplicity – UML notation

Meaning Example Notation

an exact number

exactly one exactly six

1 (or may be omitted)6

many zero or more one or more

0..* 1..*

a specific range

one to four, zero to six

1..4, 0..6,

a choice two or four or five 2, 4, 5

unspecified an arbitrary, unspecified number

*

Page 20: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

20

Aggregation – a whole-part Aggregation – a whole-part relationshiprelationship

Wheel Door Engine

Car

14 2,4,5 1

Identified by - 'consists of', 'has a', or 'is a part of‘Wheels, doors and engine are ‘part’ of a car

Page 21: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

21

Composition – tighter aggregationComposition – tighter aggregation

the whole object has exclusive ownership of its parts i.e. the part object can only participate in one aggregation;

the parts live and die with the whole

Head

Hand

Wheel

WierdoRobot

3

2

66

3

2

Page 22: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

22

Inheritance and generalizationInheritance and generalization

Inheritance – mechanism for defining a new class in terms of an existing class

Generalization – moving common attributes and operations in to a more general class

VisitorsCard

cardNumbercurrentDate

delete ()

StaffCard

cardNumbernamedeptexpiryDate

delete ()

Page 23: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

23

Inheritance and generalizationInheritance and generalization

VisitorsCard currentDate

delete( )

StaffCard name dept. expiryDate

delete( )

Card cardNumber

delete( )

• a specialized class inherits from a general class• a subclass inherits from a superclass• a child class inherits from a parent class• a derived class inherits from a base class.

Relationship described as‘is-a’‘is–a-type-of’‘is–a–kind–of’

Page 24: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

24

Abstract class – never instantiatedAbstract class – never instantiated

Card{abstract}

cardNumber

delete()

Inheritance a relationship between classes

Card objects will never be created (instantiated)

Page 25: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

25

PolymorphismPolymorphism

The term polymorphism means the ability for a single message to produce a different response depending on the class of the object to which it is sent.

Polymorphism linked with an inheritance hierarchy allows a single message to be interpreted differently by different objects.

Which method is executed will depend on which object receives the message.

A single operation may be implemented by more than one method.

Page 26: 1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.

26