Professional Documents
Culture Documents
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).
Duhok Polytechnic
University
Polymorphism Difinition
Shape
String:
ShapeName
GetArea()
Duhok Polytechnic
University
Polymorphism Types
There are two types of the polymorphism:
Static Polymorphism:
Duhok Polytechnic
University
Static Polymorphism
1. Method overloading:
Duhok Polytechnic
University
Example
A base class called (Person), which contains:
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];
foreach (Person pe in p)
{
Console.WriteLine(pe.FullName());
}
}
}
Duhok Polytechnic
University