oo concepts uml representation - softwareresearch.net · inheritance, polymorphism, dynamic binding...

38
1 Stefan Resmerita, WS2015 OO concepts UML representation Objects, Classes, Messages/Methods Inheritance, Polymorphism, Dynamic Binding Abstract Classes, Abstract Coupling

Upload: others

Post on 03-Sep-2019

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

1 Stefan Resmerita, WS2015

OO concepts

UML representation

Objects, Classes, Messages/Methods

Inheritance, Polymorphism, Dynamic Binding

Abstract Classes, Abstract Coupling

Page 2: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

2 Stefan Resmerita, WS2015

Lecture notes at:

http://www.softwareresearch.net/teaching/ws-201516/vo-software-engineering/

Page 3: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

3 Stefan Resmerita, WS2015

Objects in UML

Object notation

Martin : Person

object

object

name

object

class

An object diagram provides a run time snapshot of the

system, representing objects and the connections

between them

Page 4: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

4 Stefan Resmerita, WS2015

Object diagram

Page 5: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

5 Stefan Resmerita, WS2015

Class relationships (I)

An association can be refined by other relations

Often one models first only the fact that two classes are

related and refines later this general notation element

Association

Inheritance

Aggregation (has-a)

Dependence

Page 6: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

6 Stefan Resmerita, WS2015

Class relationships (II)

Each association can be named with a text label (like in

the ER-model)

Role names can be specified at association ends

Multiplicity can be marked at association ends

A class can have an association with itself, expressing a

relationship between objects of the same class

Class A Class B label multiplicity A

role A

multiplicity B

role B

Page 7: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

7 Stefan Resmerita, WS2015

Class relationships (III)

Multiplicity specification:

1 exactly one

* any (0 or more)

0..* any (0 or more)

1..* 1 or more

0..1 0 or 1

2..5 range of values

1..5, 9 range of values or nine

Page 8: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

8 Stefan Resmerita, WS2015

Class relationships (IV)

Example:

Page 9: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

9 Stefan Resmerita, WS2015

Inheritance

Polymorphism

Dynamic Binding

Page 10: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

10 Stefan Resmerita, WS2015

Inheritance (I)

A class defines the type of an object

If one models for example a class Customer

and a class CorporateCustomer, one expects

that each object of type CorporateCustomer to

be also of type Customer. The type

CorporateCustomer is a subtype of Customer.

Page 11: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

11 Stefan Resmerita, WS2015

Inheritance (II)

A superclass generalizes a subclass

A subclass specializes a superclass

A subclass inherits methods and

attributes of its superclass

Page 12: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

12 Stefan Resmerita, WS2015

Inheritance(III)

A subclass has the following possibilities

to specialize its behavior:

Defining new operations and attributes

Modifying existing operations

(overwriting methods of the superclass)

Flatten view:

a

iv1

iv2iv3

m1()

m2()

m3()

m4()

m1()

m4()

m5()

iv4

Page 13: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

13 Stefan Resmerita, WS2015

Inheritance (IV)

UML Notation

Customer

+checkRegularCustomer():boolean

PrivateCustomer

+checkRegularCustomer():boolean

CorporateCustomer

+checkRegularCustomer():boolean

+report(doc:ActivityReport):void

Page 14: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

14 Stefan Resmerita, WS2015

Inheritance (V)

„delta“ view Flat view

(not in standard UML!)

Page 15: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

15 Stefan Resmerita, WS2015

Inheritance and access rights

Private members of a superclass are not accessible in

subclasses

Protected members of a superclass are accessible only

in subclasses

Public members are accessible everywhere

Access rights can be specified globally for a superclass

(C++):

class R : private A{ /* ... */ };

class S : protected A{ /* ... */ };

class T : public A{ /* ... */ };

Page 16: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

16 Stefan Resmerita, WS2015

Inheritance in Java

Java supports single inheritance, where each class has

at most one superclass

The keyword is extends

Example:

public class CorporateCustomer extends Customer{

...

}

Page 17: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

17 Stefan Resmerita, WS2015

Inheritance in C++

class Base {

protected: int i;

};

class Derived_1 : private Base {

int f(Base* b) { return b->i; }

int g(Derived_1* d) { return d->i; }

};

class Derived_2 : public Base {

int f(Base* b) { return b->i; }

int g(Derived_1* d) { return d->i; }

int f1() { return i; }

};

Page 18: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

18 Stefan Resmerita, WS2015

Inheritance

Polymorphism

Dynamic Binding

Page 19: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

19 Stefan Resmerita, WS2015

Polymorphism (I)

An object type can be poly (=multiple) morph (=form).

This can be depicted in the same way as plug-

compatibility:

Objects compatible

with the plug

„Plug“-Standard

Page 20: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

20 Stefan Resmerita, WS2015

Inheritance example revisited

Customer

+checkRegularCustomer():boolean

PrivateCustomer

+checkRegularCustomer():boolean

CorporateCustomer

+checkRegularCustomer():boolean

+report(doc:ActivityReport):void

Page 21: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

21 Stefan Resmerita, WS2015

Polymorphism (II)

Objects of type CorporateCustomer (subclass) keep at

least the same contract as objects of type Customer

(superclass).

Therefore it is meaningful to consider that an object of

class Ai, which is a subclass of class A, is not only of

type Ai but also of the types given by all Ai‘s

superclasses (starting with A).

An object has not only one type. It has multiple

types, and the number of types is given by the position

of the class from which the object is generated in the

class hierarchy.

Page 22: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

22 Stefan Resmerita, WS2015

Polymorphism – Example (I)

Customer customer = new Customer();

PrivateCustomer privateCustomer = new PrivateCustomer();

CorporateCustomer corporateCustomer= new CorporateCustomer();

Customer

checkRegularCustomer() customer

PrivateCustomer

checkRegularCustomer() privateCustomer

CorporateCustomer

checkRegularCustomer() corporateCustomer

report(ActivityReport)

Page 23: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

23 Stefan Resmerita, WS2015

Customer

PrivateCustomer

CorporateCustomer

Polymorphism – Example (II)

customer = privateCustomer; // OK

checkRegularCustomer() customer

checkRegularCustomer() privateCustomer

checkRegularCustomer() corporateCustomer

report(ActivityReport)

Page 24: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

24 Stefan Resmerita, WS2015

Customer

PrivateCustomer

CorporateCustomer

Polymorphism – Example (III)

customer = corporateCustomer; // OK

checkRegularCustomer() customer

checkRegularCustomer() privateCustomer

checkRegularCustomer() corporateCustomer

report(ActivityReport)

Page 25: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

25 Stefan Resmerita, WS2015

Customer

PrivateCustomer

CorporateCustomer

Polymorphism – Example (IV)

privateCustomer = customer; // wrong

checkRegularCustomer() customer

checkRegularCustomer() privateCustomer

checkRegularCustomer() corporateCustomer

report(ActivityReport)

Page 26: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

26 Stefan Resmerita, WS2015

Customer

PrivateCustomer

CorporateCustomer

Polymorphism – Example (V)

corporateCustomer = customer; // wrong

checkRegularCustomer() customer

checkRegularCustomer() privateCustomer

checkRegularCustomer() corporateCustomer

report(ActivityReport)

Page 27: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

27 Stefan Resmerita, WS2015

Polymorphism – Example (VI)

The reason for failure is that an object which is an instance of class Customer does not understand all method calls that an object which is an instance of class CorporateCustomer understands.

(1) corporateCustomer = customer; (2) corporateCustomer.report(monthlyReport);

(1) Type mismatch: cannot convert from CorporateCustomer to Customer

(2) The method report(activityReport) is undefined for the type Customer.

Page 28: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

28 Stefan Resmerita, WS2015

Polymorphism – Example (VII)

Person

+getBirthDate():String

+getHealthHistory():String

Hospital

Investor

Company

+getDevelopmentPlan():Plan

Hotel

+Customer[ ]

+addCustomer(Customer):void

PrivateCustomer

+checkRegularCustomer():boolean

CorporateCustomer

+checkRegularCustomer():boolean

+report(doc:ActivityReport):void

Hotel

+PrivateCustomer[ ]

+CorporateCustomer[ ]

+addPrivateCustomer(PrivateCustomer):void

+addCorporateCustomer(CorporateCustomer():void

Customer

+checkRegularCustomer():boolean

Page 29: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

29 Stefan Resmerita, WS2015

Static and dynamic type

Static type

Accurately given by the declaration in the program text

Example: customer is of static type Customer

Dynamic type

The type of the referenced object at runtime

Example: after the assignment customer=corporateCustomer,

the dynamic type of customer is CorporateCustomer

A variable with a static type can have several dynamic types during

its lifetime, depending of the width and depth of the class hierarchy

Page 30: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

30 Stefan Resmerita, WS2015

Dynamic binding (I)

Dynamic binding: The compiler does not specify which method is called at runtime . The method is determined at runtime based on

The method name

The variable‘s dynamic type

Customer c;

if (i > 0) then

c = new CorporateCustomer();

else

c = new PrivateCustomer();

...

c.checkRegularCustomer();

Page 31: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

31 Stefan Resmerita, WS2015

Dynamic binding (II)

When (i > 0) is true, the variable c references an

object generated from the class CorporateCustomer (and

thus has the dynamic type CorporateCustomer). Hence,

the call to checkRegularCustomer() is linked to the

method as implemented in CorporateCustomer.

In Java, all methods are dynamically bound, except for the ones explicitly marked by using the keyword static.

In C++, by contrast, methods must be explicitly marked as dynamically bound by using the keyword virtual.

Page 32: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

32 Stefan Resmerita, WS2015

Dynamic binding (III)

Dynamic binding can be used for the plug-in concept

For example, the yellow object may implement m1()

differently than the red object

m1()

m1()

m1()

call m1

Page 33: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

33 Stefan Resmerita, WS2015

Dynamic binding exercise

public class BaseTest {

protected int protMember;

BaseTest(int i){

protMember = i;

}

public void printM(){

System.out.println("Value in base class is " + protMember);

}

}

public class DerivedA extends BaseTest{

DerivedA(int i) {

super(i+1);

}

public void printM(){

System.out.println("Value in derivedA class is " + protMember);

}

}

public class DerivedB extends BaseTest {

DerivedB(int i) {

super(i+2);

}

}

public class Worker {

BaseTest bt;

public void work(){

bt= new DerivedB(0);

bt.printM();

bt = new DerivedA(0);

bt.printM();

}

public static void main(String[] args) {

Worker wk = new Worker();

wk.work();

}

}

Page 34: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

34 Stefan Resmerita, WS2015

The diamond problem

Animal myPet = new BestPet();

myPet.talk();

This problem did not occur

in Java prior to version 8

Animal

+talk():void

BestPet

Cat

+talk():void

Dog

+talk():void

Page 35: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

35 Stefan Resmerita, WS2015

Is-A and Has-A

Typical error: Is-A instead of Has-A

Tank

BigMetal

+load():void

Cannon

+load():void

Car

+load():void

Page 36: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

36 Stefan Resmerita, WS2015

Type test and type guard in Java

Type test: Inquiry of the dynamic type

Type guard: runtime checking of type casting

Example:

if(customer instanceof CorporateCustomer){ // test

CorporateCustomer corpCust = (CorporateCustomer)customer; //guard

...

}

if(customer instanceof CorporateCustomer)

((CorporateCustomer)customer).report(monthlyReport);

Page 37: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

37 Stefan Resmerita, WS2015

Understanding

Interactions

Between Objects

Page 38: OO concepts UML representation - softwareresearch.net · Inheritance, Polymorphism, Dynamic Binding ... +report(doc:ActivityReport):void . 14 Stefan Resmerita, WS2015 Inheritance

38 Stefan Resmerita, WS2015

Object Game

Play a hotel room

reservation

scenario