290 likes | 412 Views
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:
E N D
Session 06:C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. AK - IT: Softwarekonstruktion
Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle AK - IT: Softwarekonstruktion
Software Quality Factors • The most important ones: • Reliability: • Correctness • Robustness • Modularity: • Extendibility • Reusability • This is addressed through: • Inheritance and polymorphism AK - IT: Softwarekonstruktion
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 AK - IT: Softwarekonstruktion
DoME objects AK - IT: Softwarekonstruktion
DoME object model AK - IT: Softwarekonstruktion
Class diagram View Source (dome-v1) AK - 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 AK - IT: Softwarekonstruktion
Using inheritance AK - 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 AK - IT: Softwarekonstruktion
Inheritance in C# no change here public class Item { ... } change here public class DVD : Item { ... } public class CD : Item { ... } View Source (dome-v2) AK - IT: Softwarekonstruktion
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); Subtyping Static type Dynamic type AK - 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.} AK - 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.) AK - 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. AK - IT: Softwarekonstruktion
Object diagram Static type Dynamic type AK - IT: Softwarekonstruktion
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! What we want What we have title: A Swingin' Affair (64 mins)* my favourite Sinatra album title: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie! AK - IT: Softwarekonstruktion
The inheritance hierarchy Here we only know information in Item AK - IT: Softwarekonstruktion
Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking. View Source (dome-v3) AK - 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? AK - IT: Softwarekonstruktion
Method lookup No inheritance or polymorphism. The obvious method is selected. AK - IT: Softwarekonstruktion
Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match. AK - IT: Softwarekonstruktion
Method lookup Polymorphism and overriding. The ‘first’ version found (starting at the bottom of the hierarchy) is used. AK - 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. AK - 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. AK - IT: Softwarekonstruktion
Defining and Calling an overridden method public class Item { ... public virtual void Print() { --- } ... } public class CD : Item { ... public override void Print() { base.Print(); --- } ... } AK - IT: Softwarekonstruktion
Example: • On Employee there is a method GiveBonus() which may have different implementations in the superclass and in the subclasses. View Source (EmpProjectV2.rar) AK - IT: Softwarekonstruktion
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... } AK - IT: Softwarekonstruktion
Exercises • Session06.docx AK - IT: Softwarekonstruktion