-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphism.cs
60 lines (54 loc) · 1.53 KB
/
polymorphism.cs
1
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
using System;
namespace Polymorphism
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee();
//e.PrintFullNmae();
//Polymorphism enables you to envoke derived class methods
//through base class methods at run time. if the child class
//if the child class is overriding the base class method.
Employee[] employees = new Employee[4];
employees[0] = new Employee();
employees[1] = new PartTimeEmployee();
employees[0] = new FullTimeEmployee();
employees[0] = new TemporaryEmployee();
foreach(Employee e in employees)
{
e.PrintFullNmae();
}
}
}
public class Employee
{
public string FirstName = "FN";
public string LastName = "LN";
public virtual void PrintFullNmae()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
public class PartTimeEmployee : Employee
{
public override void PrintFullNmae()
{
base.PrintFullNmae();
}
}
public class FullTimeEmployee : Employee
{
public override void PrintFullNmae()
{
Console.WriteLine(FirstName + " " + LastName+" Full Time");
}
}
public class TemporaryEmployee : Employee
{
public override void PrintFullNmae()
{
Console.WriteLine(FirstName + " " + LastName+" Temporary");
}
}
}