Session 06: C# OOP-3 - PowerPoint PPT Presentation

bevis-acevedo
session 06 c oop 3 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Session 06: C# OOP-3 PowerPoint Presentation
Download Presentation
Session 06: C# OOP-3

play fullscreen
1 / 29
Download Presentation
Session 06: C# OOP-3
115 Views
Download Presentation

Session 06: C# OOP-3

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle AK - IT: Softwarekonstruktion

  3. Software Quality Factors • The most important ones: • Reliability: • Correctness • Robustness • Modularity: • Extendibility • Reusability • This is addressed through: • Inheritance and polymorphism AK - IT: Softwarekonstruktion

  4. 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

  5. DoME objects AK - IT: Softwarekonstruktion

  6. DoME object model AK - IT: Softwarekonstruktion

  7. Class diagram View Source (dome-v1) AK - IT: Softwarekonstruktion

  8. 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

  9. Using inheritance AK - IT: Softwarekonstruktion

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Object diagram Static type Dynamic type AK - IT: Softwarekonstruktion

  17. 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

  18. The inheritance hierarchy Here we only know information in Item AK - IT: Softwarekonstruktion

  19. Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking. View Source (dome-v3) AK - IT: Softwarekonstruktion

  20. 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

  21. Method lookup No inheritance or polymorphism. The obvious method is selected. AK - IT: Softwarekonstruktion

  22. Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match. AK - IT: Softwarekonstruktion

  23. Method lookup Polymorphism and overriding. The ‘first’ version found (starting at the bottom of the hierarchy) is used. AK - IT: Softwarekonstruktion

  24. 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

  25. 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

  26. 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

  27. 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

  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... } AK - IT: Softwarekonstruktion

  29. Exercises • Session06.docx AK - IT: Softwarekonstruktion