简体   繁体   中英

same code but different output c# vs java using polymorphism?

I was playing around with polymorphism concepts. I thought that all the OOP concepts are same in every language(almost). I compiled and ran this code but java and c# gave different answers:

public class Employee
{
    // constructor implemented
    public void mailCheck()
    {
        Console.WriteLine("In Employee Method\n Mailing a check to "+ this.name+ " " + this.address);
    }

}

public class Salary : Employee
{
    //constructor implemented
    public void mailCheck()
    {
        Console.WriteLine("Within mailCheck of Salary class ");
        Console.WriteLine("Mailing check to " + getName() + " with salary " + salary);
    }
}

class driver
{
    static void Main(string[] args)
    {

        Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
        e.mailCheck();
    }
}

If I run this code in java it calls salary class method and if I run this code in c# it calls employee class method.

Why does that happen?

In Java, you don't have to write @Override in order to override a method with the same signature. It is like that by default . That is not the case in C#, as you have to prefix virtual
( public virtual void mailCheck() ) in order to signal that the method is going to be overridden.

That is why you are getting different outputs between the two languages.

In C# you are not overriding the mailCheck method. Because by redefining mailCheck into Salary class like you are doing in C# you are just hidding the function defined in the base class Employee into Salary derived class. Then the following code will not work;

Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
e.mailCheck();

Because mailCheck is hidden into Salary base class, e.mailCheck will call the base class implementation instead of the derived class implementation.

In order to use the derived class implementation you have two options:

First option:

You need to explicitly call the mailCheck on a variable declared explicity as a Salary type then you need to change your code like this:

Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
((Salary)e).mailCheck();

// Or just like this:

Salary e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
e.mailCheck();

Second option:

Like I already said, reimplementing a base class method does not mean you are overriding it into the derived class. You are just hidding this method. This is why when compiling the code into Visual Studio you will have not an error but a warning from VS that telling you need to explcitly hide this reimplemented method by using new keyword. The new keyword will tell other developpers that wil look at your code in the future to know that you don't hide the method by don't understanding what you're doing. If you don't need to hide your mailCheck method and need to take advantage of OOP polymorhism then you must mark the mailCheck method into the base with virtual modifier then you must add the override modifier to the reimplmented method into the derived class method. Your code into the Main method remains the same.

As others already said, Java does not need you to explictly mark your reimplemented methods as overriden but with C# you need to explcity say what you are trying to do by reimplementing a method liek you do otherwise it will simply hide it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM