You are on page 1of 13

CSC439: Visual Programming

Polymorphism, Dynamic Binding

Fall 2018

1
Polymorphism
 Polymorphism (from Greek, meaning “many forms”) is the
quality that allows one interface to access a general class
of actions
 Automobile steering wheel example
 Stack example in programming

2 Polymorphism, Dynamic Binding


Polymorphism in C#
 Operator overloading
 Method overloading
 Virtual methods and overriding

3 Polymorphism, Dynamic Binding


Operator Overloading
 Operator overloading is closely related to method overloading.To overload
an operator, use
the operator keyword to define an operator method.
 There are two forms of operator methods: one for unary operators and
one for binary
operators.
 // General form for overloading a unary operator
public static ret-type operator op(param-type operand)
{
// operations
}
 // General form for overloading a binary operator
public static ret-type operator op(param-type1 operand1, param-type1
operand2)
{
// operations
}

4 Polymorphism, Dynamic Binding


Example of Operator Overloading
 // A three-dimensional coordinate class.
class ThreeD {
int x, y, z; // 3-D coordinates
public ThreeD() { x = y = z = 0; }
public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }
// Overload binary +.
public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x; // These are integer additions
result.y = op1.y + op2.y; // and the + retains its original
result.z = op1.z + op2.z; // meaning relative to them.
return result;
}
}

5 Polymorphism, Dynamic Binding


Method Overloading
 In C#, two or more methods within the same class can
share the same name, as long as
their parameter declarations are different
 This process is referred to as method overloading

6 Polymorphism, Dynamic Binding


Method Overloading Example
 class Overload {
public void OvlDemo() {
Console.WriteLine("No parameters");
}
// Overload OvlDemo for one integer parameter.
public void OvlDemo(int a) {
Console.WriteLine("One parameter: " + a);
}
// Overload OvlDemo for two integer parameters.
public int OvlDemo(int a, int b) {
Console.WriteLine("Two parameters: " + a + " " + b);
return a + b;
}
}

7 Polymorphism, Dynamic Binding


Method Overloading Example
 class OverloadDemo {
static void Main() {
Overload ob = new Overload();
int resI;
double resD;
// Call all versions of OvlDemo().
ob.OvlDemo();
Console.WriteLine();
ob.OvlDemo(2);
Console.WriteLine();
resI = ob.OvlDemo(4, 6);
Console.WriteLine("Result of ob.OvlDemo(4, 6): " + resI);
}
}
 This program generates the following output:
No parameters
One parameter: 2
Two parameters: 4 6
Result of ob.OvlDemo(4, 6): 10

8 Polymorphism, Dynamic Binding


Virtual methods and overriding
 virtual method is a method that is declared as virtual in a
base class
 The defining characteristic of a virtual method is that it
can be redefined in one or more derived classes.

9 Polymorphism, Dynamic Binding


Virtual Class Example
 using System;
class Base {
// Create virtual method in the base class.
public virtual void Who() {
Console.WriteLine("Who() in Base");
}
}
class Derived1 : Base {
// Override Who() in a derived class.
public override void Who() {
Console.WriteLine("Who() in Derived1");
}
}

10 Polymorphism, Dynamic Binding


Calling Base Class Constructors
 A derived class can call a constructor defined in its base
class by using an expanded form
of the derived class’ constructor declaration and the
base keyword

11 Polymorphism, Dynamic Binding


Example
 class TwoDShape {
double pri_width;
double pri_height;
public TwoDShape(double w, double h) {
Width = w;
Height = h;
}
}
 class Triangle : TwoDShape {
string Style;
// Call the base class constructor.
public Triangle(string s, double w, double h) : base(w, h) {
Style = s;
}

12 Polymorphism, Dynamic Binding


 When a derived class object is created, whose
constructor is executed first?
 The one in the derived class or the one defined by the
base class?
 In a class hierarchy, constructors are called in order of
derivation, from base class to derived class

13 Polymorphism, Dynamic Binding

You might also like