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

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

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

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

Day 5.1. Virtual versus static methods.

2. Polymorphism3. Detailed worked example.

4. Abstract classes.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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

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

2. Polymorphism.

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

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

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.

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

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!

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

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.

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

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()

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

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.

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

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

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

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.

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

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.

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

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.

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

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