C# Polymorphism
Polymorphism means "many forms" or "multiple types". In reality, a person can be a man, a woman or a child. A shape can be a circle, a rectangle or a triangle. A physical activity may include walking, jogging or swimming. These all stand for polymorphism.
In OOP, we can declare a base class and many derived classes to demonstrate polymorphism. Virtual methods or abstract methods can be defined in the base class and overridden in the derived classes. Also, we can assign an object of a derived class to a variable of a base class and call the virtual method in the base class. At run-time, CLR will invoke the override of the virtual method of the most derived class in the class inheritance hierarchy to run.
Example 01-70-01
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System; class Person { public virtual void whoAmI() { Console.WriteLine("I'm a person."); } } class Man : Person { public override void whoAmI() { Console.WriteLine("I'm a man."); } } class Woman : Person { public override void whoAmI() { Console.WriteLine("I'm a woman."); } } public class TestPerson { static void Main() { Person a = new Man(); a.whoAmI(); Person b = new Woman(); b.whoAmI(); Console.Read(); } }
Output
I'm a man. I'm a woman.
Explanation
- Line 3-9: Define a base class Person and a virtual method whoAmI().
- Line 11-25: Define 2 derived class Man and Woman and their override method whoAmI().
- Line 29: Main method starts in the TestPerson class.
- Line 31: Declare a Person variable, create a new object of Man and assign the object to the base class variable.
- Line 32: Output the string defined in class Man instead of that in class Person.
- Line 33-34: Happened the same as that in line 31-32.
For an override method of the most derived class to be called, please check the example 01-59-02 in C# Sealed section.
Example 01-70-02
This is a good example to demonstrate the concept of polymorphism using in applications.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System; using System.Collections.Generic; abstract class Shape { public abstract void area(); } class Rectangle : Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public override void area() { Console.WriteLine("Rectangel Area: {0}", length * width); } } class Triangle : Shape { private double baseline; private double height; public Triangle(double baseline, double height) { this.baseline = baseline; this.height = height; } public override void area() { Console.WriteLine("Triangel Area: {0}", baseline * height / 2.0); } } class Circle : Shape { const double PI = 3.14; private double radius; public Circle(double radius) { this.radius = radius; } public override void area() { Console.WriteLine("Circle Area: {0}", radius * radius * PI); } } public class TestShape { static void Main() { Listshapes = new List (); Shape shape1 = new Rectangle(10, 10); shapes.Add(shape1); shapes.Add(new Circle(10)); shapes.Add(new Triangle(10, 10)); shapes.Add(new Circle(20)); foreach (Shape s in shapes) { s.area(); } Console.Read(); } }
Output
Rectangel Area: 100 Circle Area: 314 Triangel Area: 50 Circle Area: 1256
Explanation
- Line 4-7: Define a base abstract class Shape and an abstract method area().
- Line 9-57: Define 3 derived classes and the override method area().
- Line 61: Use Main() method for testing.
- Line 63: Define a generic list to hold all references of Shape class. We'll introduce C# generic in the later section.
- Line 64-65: Assign a Rectangle object to Shape variable shape1 and add shape1 to the list shapes.
- Line 66-68: Add the objects directly instead of defined a bridge variable like shape1.
- Line 70: Get each Shape s from the list shapes. The foreach statement will be introduced in details later.
- Line 72: Output the area of the shape. At run-time, the override method of the actual object will be called.
In the above example, the first step is to collect each rectangle, triangle or circle to the Shape list. The second step is to output the area of each shape s in the foreach loop statement even though we don't know which shape it is. At run-time, CLR would invoke the override method of the real object which s held. That's the polymorphism.