Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 5. 1. Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.

Similar presentations


Presentation on theme: "Day 5. 1. Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes."— Presentation transcript:

1 Day 5. 1. Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.

2 1. Virtual versus static methods. §A. Motivation. §Member methods can be either “static” or “virtual.” §What does this mean? §Intuitively, something is static if it it is fixed / cannot change, whereas a virtual feature can simulate a variety of different things.

3 Virtual versus static methods. §Analogy: §On a boat or in an airplane (and in some classrooms) the seats are bolted to the floor. Seating is “static.” §In some cars (and in many classrooms) the same seats can adopt many different configurations— many different virtual passenger areas / classrooms even though there is only one physical car / classroom.

4 Virtual versus static methods. §Unless it is upgraded, the RAM of a given machine is fixed. However, there can be a variable amount of “virtual memory” depending on demand, free drive space, etc. §By analogy, a member method is static, if and only if it is “fixed” to one class. This is the only class the method can belong to.

5 Virtual versus static methods. §But a member method is virtual if it can belong to a variety of classes, and the specific class is either determined by the compiler or not resolved until run-time.

6 Virtual versus static methods. §More technically, static member methods always use “early binding,” meaning that the method name (identifier) is bound to a particular class at compile time. §Virtual methods *may* use either early binding or late binding, meaning that the method name is bound to one or more methods at run time.

7 Why use virtual methods? §Suppose you have many similar classes e.g. within the Employ Hierarchy: §Hourly, Salaried, Contract, Temporary, Freelance, Daylight-robber, etc. §All of these classes may need similar methods e.g. they may all need Calculate and Print. §It would be nice if we could reuse the same method names throughout the hierarchy.

8 Virtual versus static methods. §To implement a virtual method: §(1) The method must be declared as virtual in the base class; §(2) A variety of versions of the method can be defined in the implementation files for the descendant classes using the keyword override. §The result is each version of the method has its own address.

9 Virtual versus static methods. §These addresses can be used either at compile time or run-time to locate whichever specific versions of the method are needed. §This allows “polymorphism.” §Specifically, it allows “polymorphic method calls.”

10 2. Polymorphism. §What does this mean? §Literally “poly” means…. §A parrot? §Polytheism = §So “poly” means…. §“Morphic” has to do with…. §So polymorphic means……

11 Polymorphism (cont.). §E.g. in Greek mythology, Proteus the shape- changer. §E.g. in biology: influenza viruses. §E.g. in computer science: a multi-platform compiler, can take same source code and generate different target programs. §E.g. in missions, same Gospel translated into many languages / cultural categories.

12 Polymorphism (cont.). §In this context, a method call is polymorphic if a syntactically identical method call can have many different meanings. The ambiguity is resolved by identifying the class to which it belongs. §Why a big deal? The same method call could be re-used for 1000 classes!

13 Polymorphism (cont.). §E.g. suppose a vast multi-national company has 1000 different types of employee, so 1000 employee classes and all need CalculatePay() and Print() method. §The same method call could be re-used for all of them, instead of having 1000 similar method calls.

14 3. Detailed worked example. §How can this be done technically? §(1) First select methods to be marked virtual in the base class (they do not all have to be). See Employee_Class_2.cs §E.g. within the Employee2 class: § public virtual void CalculatePay() § public virtual void Print()

15 Detailed worked example. §(2) In descendant classes, use the same method name marked with override: §E.g. within Hourly and Salaried, see: §public override void CalculatePay() §public override Print () §Note that the implementations are different in different classes.

16 Detailed worked example. §(3) In the driver class file (Employee_Driver_2. cs), note the polymorphic method calls. The same method name can have a different meaning depending on the class it belongs to. §In this example, early binding is used (the compiler can tell which class a method call refers to).

17 How do we do late binding? §See Employee_Driver_2A.cs. §Note the use of an Array of objects. §In this case, the meaning of the method calls for CalculatePay() and Print() is determined at run-time using the address of the class. §This is an example of late binding.

18 How is this possible? §See Employee_Class_2A.cs. §Set_Data is an example of an overloaded method in the base class. §Overloading is not the same as polymorphism because the method calls are not the same for the different versions. §Calculate_Pay() and Print () are polymorphic methods.

19 4. Abstract classes. §We mentioned before that sometimes it makes no sense for a class to have instances. No employee is just an employee, no animal is just an animal, no shape is just a shape. §To avoid someone instantiating an object of such an abstract class we can mark the class abstract.

20 Abstract classes (cont.). §As a result, the Employ class cannot be instantiated, but its descendant classes can, SO LONG AS they are not marked abstract (i.e. being abstract is not inherited).


Download ppt "Day 5. 1. Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes."

Similar presentations


Ads by Google