Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding Polymorphism

Similar presentations


Presentation on theme: "Understanding Polymorphism"— Presentation transcript:

1 Understanding Polymorphism
The syntax and examples in this lesson are specific to C#.

2 Module Learning Outcomes
Understand the models and methodologies used in software development Identify and explain the most appropriate software solution to meet a project specification, including user requirements, constraints and limitations of available resources Demonstrate the use of teamwork and software to communicate ideas and progression of agreed tasks within a commercial setting Design a prototype software solution to a given specification. Develop and test a prototype software solution to a given specification, using own programming skills. Report on how the artefact can be improved and maintained by other software developers.

3 Lesson Overview When you see code on the slides give it a try!!
Students will understand polymorphism. In this lesson, you will learn about: Extending the functionality in a class after inheriting from a base class Overriding methods in a derived class When you see code on the slides give it a try!!

4 Guiding Questions How can the function of a class be changed after inheriting from a base class? How are methods overridden? Use the guiding questions as a class starter, allowing the students time to answer the questions in their journals. Discuss student answers to the questions.

5 Activator—Predict the output
public class A { public virtual void say(){ Console.WriteLine(“A”); } } public class B : A { public override void say(){ Console.WriteLine(“B”); } public class C { public static void main() { A b = new B(); b.say(); }

6 Review Terms base—a keyword is used to access members of the base class from within a derived class. new—when used as a modifier, this keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. override—a modifier required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

7 Review Terms (continued)
polymorphism—the ability to redefine a method in a derived class (a class that inherited its data structures and methods from another class). A class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. sealed—cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. virtual—a keyword used to modify a method or property declaration, in which case the method or the property is called a virtual member. — The virtual member allows its implementation to be replaced within derived classes. Polymorphism allows the programmer to define a base class that includes routines that perform standard operations on groups of related objects, without regard to the exact type of each object. The programmer then redefines the routines in the derived class for each type, taking into account the characteristics of the object.

8 Polymorphism Refers to the ability to redefine methods in a derived class and use a class as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. Is important not only to the derived classes, but to the base classes as well. Using the base class could be the same as using an object of the derived class that has been cast to the base class type. When a derived class inherits from a base class, it gains all the methods, fields, properties, and events of the base class. To change the data and behavior of a base class, you have two choices: You can replace the base member with a new derived member, or you can override a virtual base member.

9 Using the new Keyword Replacing a member of a base class with a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property in a derived class.

10 Using the new Keyword (continued)
The new keyword is placed before the return type of a class member that is being replaced: public class BaseClass { public int WorkField; public void DoWork() { } } public class DerivedClass : BaseClass { public new int WorkField; public new void DoWork() { } }

11 Using the new keyword (continued)
When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class. DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method BaseClass A = (BaseClass) B; A.DoWork(); // Calls the old method

12 Using the virtual and override Keywords
For an instance of a derived class to completely take over a class member from a base class, the base class has to declare that member as virtual. A derived class then uses the override keyword, instead of new, to replace the base class implementation with its own. public class BaseClass { public int WorkField; public virtual void DoWork() { } } public class DerivedClass : BaseClass { public int WorkField; public override void DoWork() { } }

13 Using the virtual and override Keywords (continued)
Fields cannot be virtual; only methods, properties, events, and indexers can be virtual. When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class. DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method BaseClass A = (BaseClass) B; A.DoWork(); // Also calls the new method

14 Virtual members remain virtual
If class A declares a virtual member, class B derives from A, and class C derives from B, class C inherits the virtual member and has the option to override it, regardless of whether class B declared an override for that member. public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } } public class C : B { public override void DoWork() { } }

15 Using the sealed Keyword
A derived class can stop virtual inheritance by declaring an override as sealed In the code samples below, the method DoWork is no longer virtual to any class derived from C. It is still virtual for instances of C, even if they are cast to type B or type A. public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } } public class C : B { public sealed override void DoWork() { } }

16 Using the sealed Keyword (continued)
Sealed methods can be replaced by derived classes using the new keyword. If DoWork is called on D using a variable of type D, the new DoWork is called. If a variable of type C, B, or A is used to access an instance of D, a call to DoWork will follow the rules of virtual inheritance, routing those calls to the implementation of DoWork on class C. public class C : B { public sealed override void DoWork() { } } public class D : C { public new void DoWork() { } }

17 Lesson Review Describe situations in which the following keywords are used. Provide code to support your answer. virtual override base new sealed

18 References Kendall, K.E. and Kendall, J.E. (2011) Systems Analysis and Design. 8. ed., global ed. Harlow: Pearson Education Microsoft (2015) C# Programming Guide [online] available from < [27 June 2016] Robertson, L.A., Course Technology: Cengage Learning, and Cengage Learning (2003) Simple Program Design: A Step-by-Step Approach. Australia: Course Technology : Cengage Learning Watson, K. (ed.) (2013) Beginning Visual C# 2012 Programming. Wrox beginning guides. Indianapolis, IN: John Wiley & Sons Wiley (2012) Software Development Fundamentals: Exam Hoboken, NJ: John Wiley & Sons Inc


Download ppt "Understanding Polymorphism"

Similar presentations


Ads by Google