1

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:

  1. Terry Wignfield
  2. Terry Wingfield
  3. Terry Wingfield

But instead the below was written to the console:

  1. Terry Wingfield
  2. John Doe
  3. 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,

fubo
  • 44,811
  • 17
  • 103
  • 137
Tez Wingfield
  • 2,129
  • 5
  • 26
  • 46
  • 1
    This is because `writeName()` in the child classes is _hiding_ the version in the base class (you should have seen a compiler warning about this). If you cast all three to `Employee`s, you should see the first result. – JLRishe Aug 27 '14 at 13:51

4 Answers4

5

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.

Igor
  • 15,833
  • 1
  • 27
  • 32
  • i'll mark this as answered considering you supplied code with your answer. If i may ask, to help future "readers". Why do you need to reference the base class to instantiate a child class for polymorphic behaviour? Thank you. – Tez Wingfield Aug 27 '14 at 18:52
  • Thank you. A great informative update to go with your answer. – Tez Wingfield Aug 28 '14 at 07:22
4

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

Community
  • 1
  • 1
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • This doesn't answer his question. He was asking about polymorphism with method implementations and why it was calling the base class method's implementation when he wasn't using override. – Alan Aug 27 '14 at 13:57
  • @Alan Method hiding does explain the behavior he was seeing. – JLRishe Aug 27 '14 at 13:59
  • 1
    His question was why he doesn't get the base class's method's implementation without using the override. The correct answer is, because he isn't using a reference to the base class type. He is trying to test the difference between using virtual methods or not. And by using variables of the base types (his error) he cannot demonstrate that regardless of whether it is method hiding or not. – Alan Aug 27 '14 at 14:01
  • @Selman22 Thank for your comment but this really didn't answer my question. It is certainly apart of the problem but not the whole. Never the less, i appreciate all time given to help my solve the issue. – Tez Wingfield Aug 27 '14 at 18:44
0

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();
        }
    }
sm_
  • 2,572
  • 2
  • 17
  • 34
  • I see that you've added the Virtual/Override keyword(s) but no mention of referencing the base class, when instantiating the child classes. Thank you for your time. – Tez Wingfield Aug 27 '14 at 18:46
0

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.

  • great informative comment but may be sightly of topic. I understand what is happening and what was supposed to be outputted but there was a "bug" typo in the code. Still a great comment! will help future "readers". Thank you. – Tez Wingfield Aug 27 '14 at 18:50