Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN 2013-03-121AK - IT: Softwarekonstruktion."— Presentation transcript:

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

2 FEN 2013-03-12AK - IT: Softwarekonstruktion2 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle

3 Software Quality Factors FEN 2013-03-12AK - IT: Softwarekonstruktion3 The most important ones: –Reliability: –Correctness –Robustness –Modularity: –Extendibility –Reusability This is addressed through: –Inheritance and polymorphism

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 FEN 2013-03-124AK - IT: Softwarekonstruktion

5 DoME objects FEN 2013-03-125AK - IT: Softwarekonstruktion

6 DoME object model FEN 2013-03-126AK - IT: Softwarekonstruktion

7 Class diagram View Source (dome-v1)dome-v1 FEN 2013-03-127AK - 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 FEN 2013-03-128AK - IT: Softwarekonstruktion

9 Using inheritance FEN 2013-03-129AK - 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 FEN 2013-03-1210AK - IT: Softwarekonstruktion

11 Inheritance in C# public class Item {... } public class CD : Item {... } public class DVD : Item {... } no change here change here View Source (dome-v2)dome-v2 FEN 2013-03-1211AK - IT: Softwarekonstruktion

12 Subtyping First, we had: public void AddCD( CD theCD) public void Add DVD ( DVD the DVD ) Now, we have: public void AddItem( Item theItem) We call this method with: DVD dvd = new DVD (...) ; myDB.AddItem(my DVD ); Static type Dynamic type FEN 2013-03-1212AK - 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. } FEN 2013-03-1213AK - 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.) FEN 2013-03-1214AK - 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. FEN 2013-03-1215AK - IT: Softwarekonstruktion

16 Object diagram Static type Dynamic type FEN 2013-03-1216AK - 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! 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-1217AK - IT: Softwarekonstruktion

18 The inheritance hierarchy Here we only know information in Item FEN 2013-03-1218AK - IT: Softwarekonstruktion

19 Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking. View Source (dome-v3)dome-v3 FEN 2013-03-1219AK - 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? FEN 2013-03-1220AK - IT: Softwarekonstruktion

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

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

23 Method lookup Polymorphism and overriding. The ‘first’ version found (starting at the bottom of the hierarchy) is used. FEN 2013-03-1223AK - 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. FEN 2013-03-1224AK - 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. FEN 2013-03-1225AK - IT: Softwarekonstruktion

26 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-1226AK - IT: Softwarekonstruktion

27 FEN 2013-03-12AK - IT: Softwarekonstruktion27 Example: On Employee there is a method GiveBonus() which may have different implementations in the superclass and in the subclasses. View Source (EmpProjectV2.rar)EmpProjectV2.rar

28 FEN 2013-03-12AK - IT: Softwarekonstruktion28 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... }

29 FEN 2013-03-12AK - IT: Softwarekonstruktion29 Exercises Session06.docx


Download ppt "Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN 2013-03-121AK - IT: Softwarekonstruktion."

Similar presentations


Ads by Google