On my quest to understand Polymorphism more, i have constructed a little test and it's returning unexpected results.
So the idea was to override the base class method with virtual/override keywords but it seems i don't need those ?
public class Employee
{
public Employee()
{
this.firstName = "Terry";
this.lastName = "Wingfield";
}
public string firstName { get; set; }
public string lastName { get; set; }
public void writeName()
{
Console.WriteLine(this.firstName + " " + this.lastName);
Console.ReadLine();
}
}
public class PartTimeEmployee : Employee
{
public void writeName()
{
Console.WriteLine("John" + " " + "Doe");
Console.ReadLine();
}
}
public class FullTimeEmployee : Employee
{
public void writeName()
{
Console.WriteLine("Jane" + " " + "Doe");
Console.ReadLine();
}
}
static void Main(string[] args)
{
Employee employee = new Employee();
PartTimeEmployee partTimeEmployee = new PartTimeEmployee();
FullTimeEmployee fullTimeEmployee = new FullTimeEmployee();
employee.writeName();
partTimeEmployee.writeName();
fullTimeEmployee.writeName();
}
}
With the code above i was expecting results like so:
- Terry Wignfield
- Terry Wingfield
- Terry Wingfield
But instead the below was written to the console:
- Terry Wingfield
- John Doe
- Jane Doe
I assumed the latter would not work because it would of needed the ovrride keyword.
So the question is why am i seeing the latter names without the appropriate keywords?
I hope this is clear enough to read.
Regards,