You are on page 1of 11

Duhok Polytechnic University

.Information Technology Management Dept


2nd Year Students

Object Oriented Programming


Lecture#8

Zeravan Arif
zeravan.ali@dpu.edu.krd

Duhok Polytechnic
University
Outline

 What is Polymorphism.
 Type of Polymorphism.
 Static Polymorphism.
 Dynamic Polymorphism.
 Example.

Duhok Polytechnic
University
What is Polymorphism
 Polymorphism is one of the principles of OOP. The “Poly”
means many, and the “Morph” means forms. Polymorphism
also refers to one name with many forms (one name with
multiple functionality).

 With polymorphism class objects belonging to the same


hierarchical tree may have function with the same name,
but with different behaviours.

Duhok Polytechnic
University
Polymorphism Difinition
Shape

String:
ShapeName
GetArea()

Rectangle Triangle Circle

Int: length, width Double base, Int radius


height
GetArea() GetArea()
GetArea()

Duhok Polytechnic
University
Polymorphism Types
 There are two types of the polymorphism:

 Static Polymorphism, and


 Dynamic Polymorphism

 Static Polymorphism:

 It is also called as Compile-Time polymorphism.


Method overloading is a technique for the static
polymorphism.

Duhok Polytechnic
University
Static Polymorphism
1. Method overloading:

 Multiple definitions for the same method in the same


class.
 These definitions must be different in number of
parameters or data types or sequence.
 These definition might or might not be different in return
type.
» public int Add(int a, int b) { return a + b; }
» public int Add(int a, int b, int c) { return a + b + c; }
» public float Add(int a, float b) { return a + b; }
Duhok Polytechnic
University
Dynamic Polymorphism
 Dynamic polymorphism (Run-Time)

 In C# an override keyword with abstract or virtual is used

to create dynamic polymorphism.


 The override (Re-defined) modifier is required to extend or

modify the abstract or virtual implementation of an inherited


method.
 The virtual keyword is used to modify a method declaration

and allow for it to be overridden in a derived class.

Duhok Polytechnic
University
Example
 A base class called (Person), which contains:

- Two fields (FirstName and LastName).

- Provide a necessary property for fields.

- A virtual method called (FullName) to return the full name of


person, and its overridden in the subclass.

 A derived class (Engineer), it contains an override method.

 A derived class called (Lawyer), it contains an override method.

 Create the necessary objects for all classes.

Duhok Polytechnic
University
Example
 class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
 
public virtual string FullName()
{
return FirstName + " " + LastName;
}
}
 class Engineer :Person
{
public override string FullName()
{
return “Eng." +FirstName;
}
}
 class Lawyer :Person
{
public override string FullName()
{
return "Mr." + LastName;
}
Duhok Polytechnic
}
University
Example
 class Program
{
static void Main(string[] args)
{
Engineer engineer = new Engineer();
engineer.FirstName = "Ali";
engineer.LastName = "Ahmed";
Console.WriteLine(engineer.FullName());
 
Lawyer lawyer = new Lawer();
lawyer.FirstName = "Ala";
lawyer.LastName = "Ameen";
Console.WriteLine(lawyer.FullName());
 
Person person = new Person();
person.FirstName = “Human";
person.LastName = “Being";
Console.WriteLine(person.FullName());
Console.Read();
}
Duhok Polytechnic
University }
Example
 class Program
{
static void Main(string[] args)
{
Person[] p = new Person[3];

p[0] = new Person();


p[0].FirstName = "Ali";
p[0].LastName = "Ahmed";
p[1] = new Engineer(); Create an array of objects for
p[1].FirstName = "Ala"; both base class as well as for
p[1].LastName = "Ameen"; derived classes.
p[2] = new Lawer();
p[2].FirstName = "Awaz";
p[2].LastName = "Muhammed";

foreach (Person pe in p)
{
Console.WriteLine(pe.FullName());
}
}
}
Duhok Polytechnic
University

You might also like