session 07: c# oop 4 review of: inheritance and polymorphism. static and dynamic type of an object....

25
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-01 1 AK - IT: Softwarekonstruktion

Upload: jonah-doyle

Post on 18-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Session 07:C# OOP 4

Review of:Inheritance and Polymorphism.

Static and dynamic type of an object.

Abstract Classes.

Interfaces.

FEN 2013-04-01 1AK - IT: Softwarekonstruktion

Page 2: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 2

Object-Oriented Programming

“ The Three Pillars of OOP”:Encapsulation

InheritancePolymorphism

The Substitution Principle

Page 3: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Recall: Quality Factors

FEN 2013-04-01 AK - IT: Softwarekonstruktion 3

• The most important ones:– Reliability:

– Correctness – Robustness

– Modularity:– Extendibility– Reusability

• This is addressed through:

– Inheritance and polymorphism

Page 4: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 4

Inheritance in C#

• Every method and property is inherited – constructors are not.

• private members are inherited, but are not directly accessible in the subclass.

• Every protected member of the base class is visible in subclasses, but hidden from other parts of the program.

• Members may be added in the subclass. • Multiple inheritance is not supported (by classes).• In C# every class inherits from Object.

Page 5: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 5

OO-Principles -inheritance

• Supports code-reuse.• Existing classes may be extended through

inheritance.• Inheritance is to used as type specialisation, that is:

we are modelling an “is-a” relationship between super- and subclass. For instance: CheckAccount is an Account, a special kind of account, but an account.

• So when we want to add specialised functionality to our system, inheritance may used by adding specialised classes.

• E.g.: CheckAccount may inherit BankAccount.

Page 6: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 6

Inheritance - redefining methods

• A method inherited from the base-class may be redefined (“overridden”) in the sub-class.

• For instance the Withdraw-method on Account/CheckAccout.

• In C# the must be specified “virtual” in the base-class and “override” in sub-class.

• The method in the sub-class has the same signature (name and parameter list) and the same return type as the method in the base-class.

• In the sub-class the redefined method in the base-class may be called using

base.methodName();

Page 7: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 7

Inheritance- polymorphism

• All reference variables in C# may refer to objects of subtypes.

• In the case of virtual methods it is first at execution time it is determined which method exactly is to be called.

• The method called is the one defined on the object that the reference currently is referring to.

• This is called dynamic binding – the call is bound to an actual code segment at runtime (dynamically).

Page 8: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 8

Polymorphism/Dynamic Binding

The way it used to be: Employee programmer = new Employee(“H. Acker","Programmer",22222);

Static type = Dynamic type

Static method call

Static type

Dynamic type

Using polymorphism: Employee boss = new Manager(”Big Boss",”CEO",52525, 500);

Dynamic type must be the same or a sub-type of the static type.

The compiler checks method-calls on the static type. Runtime the call is bonded to the dynamic type (dynamic binding).Dynamic binding requires methods to be specified virtual in the base-class and explicitly overridden in the sub-classes.

Dynamic type

Statisc type

Page 9: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion9

Example

• Let’s look at the implementation of the model from earlier

Manager

noOfOpts

SalesPerson

sale

Employee

namesaleryposition

WorksOn

hours0..*1 0..*1

Project

namedepartment

10..* 10..*

Now: Employees at the cantina get double bonus.View Source (EmpProjectV2.rar)

Page 10: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 10

Exercises – Solutions?

• ..\lektion06\Session06.docx

Page 11: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Abstract Classes

• In many cases we have an inheritance hierarchy where every subclass must implement the same method, but differently.

• In this case we would like to declare the signature of the method in the base class, so that the compiler is able to perform static checking of calls to the method.

• This can be done by declaring the method abstract.• When a class has one or more abstract methods, the

class it selves must be declared abstract.

FEN 2013-04-01 AK - IT: Softwarekonstruktion 11

Page 12: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Class Diagram and Code

In Client:

AbstractBase a = new Concrete1();

AbstractBase b= new Concrete2();

a.foo();

b.foo();

FEN 2013-04-01 AK - IT: Softwarekonstruktion 12

The dynamic type must be a subtype of the static type

The compiler checks that foo() is

defined on the static type

Dynamic method lookup finds the right

implementation of foo()

Page 13: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

In C#:

• The definition of an abstract class

FEN 2013-04-01 AK - IT: Softwarekonstruktion 13

• The implementation

Page 14: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

In C#:

• In the client:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 14

Cannot instantiate an abstract class.

Page 15: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Exercise

• Exercise 1 on Session07.docx.

FEN 2013-04-01 AK - IT: Softwarekonstruktion 15

Page 16: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 16

Inheritance - abstract classes

• A class that defines one or more methods that are not implemented is called abstract. And the non-implemented methods are specified abstract.

• An abstract class can not be instantiated. It can only serve as base-class.

• An abstract class can only be used as static type, not dynamic type for object.

• Abstract methods must be implemented in the sub-classes. Otherwise the subclass itself becomes abstract.

• So an abstract method defines or specifies functionality (but does not implement) what must be implemented in the subclasses.

• Constructors in an abstract class are only used by the constructors in the subclasses

Page 17: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 17

Inheritance - design considerations

• If we need a class to hold a list of employees, and it should be possible to add employees at the end of list, but nowhere else. Should we inherit Array or List or…?

• We are not to inherit at all!!! But use delegation.

• Inheritance is not to be used senselessly trying to reuse code! Inheritance is to be seen as sub typing!

• Inheritance models “is-a” relationships.

• Code-reuse is obtainable by using existing classes (composition, delegation etc.)

Page 18: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces• An interface may be seen as a purely abstract class.

– Interface: only method signatures, no implementation! (All methods are abstract)

• An interface represents a design.• Example:

– Using interfaces in the Dome example:– (In C# interfaces are

normally named “ISomething” – here IItem)

FEN 2013-04-01 18AK - IT: Softwarekonstruktion

Page 19: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces• The Item must implement

the interface (inherit it):

FEN 2013-04-01 19AK - IT: Softwarekonstruktion

Page 20: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces• The interface is now

used as static type:

FEN 2013-04-01 20AK - IT: Softwarekonstruktion

Page 21: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces• We can also create an interface to the DomeDatabase

class:

FEN 2013-04-01 21AK - IT: Softwarekonstruktion

Using the IItem interface as static type

Page 22: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces• DomeDatabase class

must implement the interface:

FEN 2013-04-01 22AK - IT: Softwarekonstruktion

Page 23: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Interfaces

• In the main program:

FEN 2013-04-01 23AK - IT: Softwarekonstruktion

We only need to know the interface

as static type

Page 24: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Why use interfaces?

• Formalise system design before implementation– especially useful with large systems.

• Contract-based programming – the interface represents the contract between client and object.

• Low coupling!– decouples specification and implementation.– facilitates reusable client code.– client code will work with both existing and future objects as long as

the interface is not changed.• Multiple inheritance

– A class may implement several interfaces, but only inherit one class. (No competing implementations).

FEN 2013-04-01 24AK - IT: Softwarekonstruktion

Page 25: Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

FEN 2013-04-01 AK - IT: Softwarekonstruktion 25

Exercises

• Exercise 2 and 3 on Session07.docx.