Reputation: 2251
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:
But instead the below was written to the console:
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,
Upvotes: 1
Views: 327
Reputation: 15893
There is no polymorphism in play in the code you showed.
Change it to:
Employee employee = new Employee();
Employee partTimeEmployee = new PartTimeEmployee();
Employee fullTimeEmployee = new FullTimeEmployee();
and you will get your expected result.
Update:
The concept of "polymorphism" (many forms) in OOP means that the code deals with references of certain type (base class or interface) while there may be instances of different types (descendants, implementations) behind these references. For polymorphism to "kick in" there must be inheritance and virtual methods (different terms in the case of interface implementation, but let's use terms relevant to your code example). You have inheritance, but no virtual methods. For regular (non-virtual) methods, method calls are resolved at compile-time based on the type of objects whose methods are called.
For code:
PartTimeEmployee partTimeEmployee = ...;
partTimeEmployee.writeName();
it is clear to the compiler what method writeName
to call, and it is PartTimeEmployee.writeName
.
Similarly, for code:
Employee partTimeEmployee = ...;
partTimeEmployee.writeName();
the method to call is Employee.writeName
.
Upvotes: 5
Reputation: 21
With C# you can rewrite a method of a base class using the "new" keyword. If you omit this keyword the compiler will compile your source code as if it exists. So the question, i think, is: what's the difference between new and override? It's a big difference.
"new" instructs the compiler to use your implementation instead of the base class implementation, but any code that is not referencing directly your class will use the base class implementation.
"override" is used on virtual and abstract methods. This instructs the compiler to use the last defined implementation of a method. If the method is called on a reference to the base class, it will use the last implementation overriding it on that object.
I hope I've been clear enough.
Upvotes: 0
Reputation: 2602
For polymorphic behavior, its a must that you override
those methods in PartTimeEmployee
and FullTimeEmployee
. What you did back there was hiding the methods in the base class.
public class Employee
{
public Employee()
{
this.firstName = "Terry";
this.lastName = "Wingfield";
}
public string firstName { get; set; }
public string lastName { get; set; }
public virtual void writeName()
{
Console.WriteLine(this.firstName + " " + this.lastName);
Console.ReadLine();
}
}
public class PartTimeEmployee : Employee
{
public override void writeName()
{
Console.WriteLine("John" + " " + "Doe");
Console.ReadLine();
}
}
public class FullTimeEmployee : Employee
{
public override void writeName()
{
Console.WriteLine("Jane" + " " + "Doe");
Console.ReadLine();
}
}
Upvotes: 0
Reputation: 101681
This is called method hiding. You are simply hiding the base class method in your derived classes. You should be getting a warning for that but it's completely legal. For more information see the documentation. You might also want to take a look at this question
Upvotes: 4