session 06: c# oop-3 inheritance and polymorphism. static and dynamic type of an object. fen...
DESCRIPTION
Software Quality Factors FEN AK - IT: Softwarekonstruktion3 The most important ones: –Reliability: –Correctness –Robustness –Modularity: –Extendibility –Reusability This is addressed through: –Inheritance and polymorphismTRANSCRIPT
Session 06:C# OOP-3
Inheritance and Polymorphism. Static and dynamic type of an object.
FEN 2013-03-12 1AK - IT: Softwarekonstruktion
FEN 2013-03-12 AK - IT: Softwarekonstruktion 2
Object-Oriented Programming
“ The Three Pillars of OOP”:Encapsulation
InheritancePolymorphism
The Substitution Principle
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
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
DoME objects
FEN 2013-03-12 5AK - IT: Softwarekonstruktion
DoME object model
FEN 2013-03-12 6AK - IT: Softwarekonstruktion
Class diagram
View Source (dome-v1)
FEN 2013-03-12 7AK - IT: Softwarekonstruktion
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
Using inheritance
FEN 2013-03-12 9AK - IT: Softwarekonstruktion
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
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
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
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
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
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
Object diagram
Static type
Dynamic type
FEN 2013-03-12 16AK - IT: Softwarekonstruktion
Conflicting outputCD: 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
The inheritance hierarchy
Here we only know information in Item
FEN 2013-03-12 18AK - IT: Softwarekonstruktion
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
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
Method lookup
No inheritance or polymorphism.
The obvious method is selected.FEN 2013-03-12 21AK - IT: Softwarekonstruktion
Method lookup
Inheritance but no overriding. The
inheritance hierarchy is ascended, searching for
a match.FEN 2013-03-12 22AK - IT: Softwarekonstruktion
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
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
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
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
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.
ManagernoOfOpts
SalesPersonsale
Employeenamesaleryposition
WorksOnhours
0..*1 0..*1
Projectnamedepartment10..* 10..*
View Source (EmpProjectV2.rar)
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... }
FEN 2013-03-12 AK - IT: Softwarekonstruktion 29
Exercises
• Session06.docx