My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com









Saturday, May 13, 2017

Method hiding Vs overriding in C# : New feature that can be cause of big troubles later

In here, I tried to show a new feature in C# or Visual studio languages. This is not in Java, and I have proactively tested that ;)

Example: Class A has a Print method; class B inherits from class A and implements the Print method as well. Now Print method will be overridden. Simple!

But now, test carefully that what happens if you change the Print method signature in class B. If you add the new keyword there, a behavior changes. In this case the method does not overrides. In fact it will hide the method. For references, i have the Github repository up and running: https://github.com/Anwar-Faiz/Method-Hiding-Demo

Most Important: In normal object calls, this one will not be caught. The behavior difference is seen when you make an object with parent class variable.


Let us start with defining some classes, and see a working example:

class A
    {
        public string Print()
        {
            return "A";
        }
    }

    class B : A
    {
        public string Print()
        {
            return "B";
        }
    }

    class A2
    {
        public string Print()
        {
            return "A2";
        }
    }

    class B2 : A2
    {
        public new string Print()
        {
            return "B2";
        }
    }

Now let us write a program to check the behavior in both ways. See attached screenshot:



The result of the run is as follows:


Demo of overriding...
 a.Getname : A
 b.Getname : B
 a2.Getname : A2
 b2.Getname : B2

Demo of method hiding...
 x.Getname : A
 y.Getname : A2



Now this clearly shows that in the second case, the method of child class becomes hidden!


Issues seen with this new feature in Visual Studio C#:
What I foresee here are a couple of problems and i am describing the same:
1. Programmers of other languages reading this part of code, will have a tough time understanding what is happening.
2. There is a huge difference between the way overriding works and this method hiding functionality behaves, this new feature may prove to be step going back to Non-OOP days!
3. More importantly, the function written in the child class, gets of no use now. It becomes hidden, in a way.
4. I further see that it is default behavior in C#. This means that you do not even need to write the new keyword. Also, if the method on the base class is marked as virtual, and you forget to mark the method on the inheriting class with override, you might land in method hiding.
5. If a bug is encountered, it will be hard to find that the trouble is cause of a new keyword.


Proposed solution to team Microsoft working on this:
As we know, since already introduced, it will be tough to remove it. Particularly, because many have already started using in their projects. I know the team Microsoft spends a lot of effort on giving backwards compatibilty, and they are really good at that!.So, the best option is to introduce the Warning on Visual Studio Editor. You can also raise a compile time warning.

PS: You can download working repository of code sample at:
https://github.com/Anwar-Faiz/Method-Hiding-Demo


Have a happy coding and testing guys!
@Anwar Faiz


No comments:

Post a Comment