session 06: c# oop-3

29
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN 2013-03-12 1 AK - IT: Softwarekonstruktion

Upload: bevis-acevedo

Post on 02-Jan-2016

49 views

Category:

Documents


2 download

DESCRIPTION

Session 06: C# OOP-3. Inheritance and Polymorphism. Static and dynamic type of an object. Object-Oriented Programming. “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle. Software Quality Factors. The most important ones: Reliability: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Session 06: C# OOP-3

Session 06:C# OOP-3

Inheritance and Polymorphism. Static and dynamic type of an object.

FEN 2013-03-12 1AK - IT: Softwarekonstruktion

Page 2: Session 06: C# OOP-3

FEN 2013-03-12 AK - IT: Softwarekonstruktion 2

Object-Oriented Programming

“ The Three Pillars of OOP”:Encapsulation

InheritancePolymorphism

The Substitution Principle

Page 3: Session 06: C# OOP-3

Software Quality Factors

FEN 2013-03-12 AK - IT: Softwarekonstruktion 3

• The most important ones:– Reliability:

– Correctness – Robustness

– Modularity:– Extendibility– Reusability

• This is addressed through:

– Inheritance and polymorphism

Page 4: Session 06: C# OOP-3

The DoME example

"Database of Multimedia Entertainment"

• stores details about CDs and DVDs– CD: title, artist, # tracks, playing time, got-it,

comment– DVD: title, director, playing time, got-it, comment

• allows (later) to search for information or print lists

FEN 2013-03-12 4AK - IT: Softwarekonstruktion

Page 5: Session 06: C# OOP-3

DoME objects

FEN 2013-03-12 5AK - IT: Softwarekonstruktion

Page 6: Session 06: C# OOP-3

DoME object model

FEN 2013-03-12 6AK - IT: Softwarekonstruktion

Page 7: Session 06: C# OOP-3

Class diagram

View Source (dome-v1)

FEN 2013-03-12 7AK - IT: Softwarekonstruktion

Page 8: Session 06: C# OOP-3

Critique of DoME

• code duplication– CD and DVD classes very similar (large

part are identical)– makes maintenance difficult/more work– introduces danger of bugs through

incorrect maintenance

• code duplication also in Database class

FEN 2013-03-12 8AK - IT: Softwarekonstruktion

Page 9: Session 06: C# OOP-3

Using inheritance

FEN 2013-03-12 9AK - IT: Softwarekonstruktion

Page 10: Session 06: C# OOP-3

Using inheritance

• define one base or super class: Item

• define subclasses for DVD and CD

• the super class defines common attributes

• the subclasses inherit the super class attributes

• the subclasses add own attributes

FEN 2013-03-12 10AK - IT: Softwarekonstruktion

Page 11: Session 06: C# OOP-3

Inheritance in C#

public class Item{ ...}

public class CD : Item{ ...}

public class DVD : Item { ...}

no change here

change here

View Source (dome-v2)

FEN 2013-03-12 11AK - IT: Softwarekonstruktion

Page 12: Session 06: C# OOP-3

Subtyping

First, we had:public void AddCD(CD theCD)public void AddDVD(DVD theDVD)

Now, we have:public void AddItem(Item theItem)

We call this method with:DVD dvd = new DVD(...);

myDB.AddItem(myDVD);

Static type

Dynamic type

FEN 2013-03-12 12AK - IT: Softwarekonstruktion

Page 13: Session 06: C# OOP-3

Static and dynamic type

• The declared type of a variable is its static type.• The type of the object a variable refers to is its

dynamic type.• The compiler’s job is to check for static-type

violations.

foreach(Item item in items) { item.Print(); // Item must have

// declared a Print method.}

FEN 2013-03-12 13AK - IT: Softwarekonstruktion

Page 14: Session 06: C# OOP-3

Subclasses and subtyping

• Classes define types.

• Subclasses define subtypes.

• Objects of subclasses can be used where objects of supertypes are required.(This is called substitution .)

FEN 2013-03-12 14AK - IT: Softwarekonstruktion

Page 15: Session 06: C# OOP-3

Polymorphic variables

• Object variables in C# are polymorphic.

(They can reference objects of more than one type.)

• They can reference objects of the declared type, or of subtypes of the declared type.

FEN 2013-03-12 15AK - IT: Softwarekonstruktion

Page 16: Session 06: C# OOP-3

Object diagram

Static type

Dynamic type

FEN 2013-03-12 16AK - IT: Softwarekonstruktion

Page 17: Session 06: C# OOP-3

Conflicting output

CD: A Swingin' Affair (64 mins)* Frank Sinatra tracks: 16 my favourite Sinatra album DVD: O Brother, Where Art Thou? (106 mins) Joel & Ethan Coen The Coen brothers’ best movie!

title: A Swingin' Affair (64 mins)* my favourite Sinatra album

title: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie!

What we want

What we have

FEN 2013-03-12 17AK - IT: Softwarekonstruktion

Page 18: Session 06: C# OOP-3

The inheritance hierarchy

Here we only know information in Item

FEN 2013-03-12 18AK - IT: Softwarekonstruktion

Page 19: Session 06: C# OOP-3

Overriding: the solution

print method in both super-

and subclasses.

Satisfies both static and

dynamic type checking.

View Source (dome-v3)

FEN 2013-03-12 19AK - IT: Softwarekonstruktion

Page 20: Session 06: C# OOP-3

Overriding

• Superclass and subclass define methods with the same signature.

• Each has access to the fields of its class.• Superclass satisfies static type check.• Subclass method is called at runtime – it

overrides the superclass version.• What becomes of the superclass version?

FEN 2013-03-12 20AK - IT: Softwarekonstruktion

Page 21: Session 06: C# OOP-3

Method lookup

No inheritance or polymorphism.

The obvious method is selected.FEN 2013-03-12 21AK - IT: Softwarekonstruktion

Page 22: Session 06: C# OOP-3

Method lookup

Inheritance but no overriding. The

inheritance hierarchy is ascended, searching for

a match.FEN 2013-03-12 22AK - IT: Softwarekonstruktion

Page 23: Session 06: C# OOP-3

Method lookup

Polymorphism and overriding. The ‘first’

version found (starting at the

bottom of the hierarchy) is used.FEN 2013-03-12 23AK - IT: Softwarekonstruktion

Page 24: Session 06: C# OOP-3

Method lookup summary

• The variable is accessed.• The object stored in the variable is found.• The class of the object is found.• The class is searched for a method match.• If no match is found, the superclass is

searched.• This is repeated until a match is found, or the

class hierarchy is exhausted.• Overriding methods take precedence.

FEN 2013-03-12 24AK - IT: Softwarekonstruktion

Page 25: Session 06: C# OOP-3

Call to base in methods

• Overridden methods are hidden ...

• ... but we often still want to be able to call them.

• An overridden method can be called from the method that overrides it.– base.Method(...)– Compare with the use of base in

constructors.FEN 2013-03-12 25AK - IT: Softwarekonstruktion

Page 26: Session 06: C# OOP-3

Defining and Calling an overridden method

public class CD : Item{ ... public override void Print() { base.Print(); --- } ...}

public class Item{ ... public virtual void Print() {

--- } ...}

FEN 2013-03-12 26AK - IT: Softwarekonstruktion

Page 27: Session 06: C# OOP-3

FEN 2013-03-12 AK - IT: Softwarekonstruktion 27

Example:

• On Employee there is a method GiveBonus() which may have different implementations in the superclass and in the subclasses.

Manager

noOfOpts

SalesPerson

sale

Employee

namesaleryposition

WorksOn

hours0..*1 0..*1

Project

namedepartment

10..* 10..*

View Source (EmpProjectV2.rar)

Page 28: Session 06: C# OOP-3

FEN 2013-03-12 AK - IT: Softwarekonstruktion 28

C# - overriding pre-defined methods- When are objects equal?

• Classes ought to override the Equals-method inherited from Object

public class Customer{ . . .

public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // surely not equal

other = (Customer) obj; // typecast to gain access return this.id == other.id; // equal, if ids are... }

Page 29: Session 06: C# OOP-3

FEN 2013-03-12 AK - IT: Softwarekonstruktion 29

Exercises

• Session06.docx