Inheritance and Function Hiding in C#

Inheritance is one of the key features in object oriented programming languages. It offers some powerful concepts but it also has some pitfalls. Inheritance allows you to combine the behaviors of different classes. So you may combine classes which have functions with the same name but different behaviors. This may result in errors which are not easy to find.

Multiple inheritance

One of the most common examples to describe this issues is multiple inheritance. For example you want to write a class C which inherits from A and B. In both classes A and B the function MyFunction is implemented. What will happen when you call C.MyFunction? Which of the both inherited functions will be used? That’s a problematic issue and the designers of C# decided to not support multiple inheritance in C# because the disadvantages of this feature will be larger than the advantages.

Function Hiding

But a second issue is function hiding. Even if you only have single inheritances it is possible to have multiple implementations of one function. The following source code shows a class A with two functions and a class B which inherits A.

class A
    public int MyX()
        return 42;

    public int MyY()
        return 3 * MyX();

class B : A
    public new int MyX()
        return 30;

As you can see, the function MyY in class A calls the function MyX. If MyY in class A is called, the result will be 126 (3*42). But what will happen when MyY of class B is called? B has a new implementation of MyX which hides the implementation done in class A. But class B does not have a function MyY and therefore the function in class A will be used.

Therefore the big question of this example is: which of the both implementation of MyX is used?

The following source code shows a console application to test this behavior.

class Program
    static void Main(string[] args)
        A a = new A();
        B b = new B();

        Console.WriteLine("A.MyX: " + a.MyX().ToString());
        Console.WriteLine("B.MyX: " + b.MyX().ToString());

        Console.WriteLine("A.MyY: " + a.MyY().ToString());
        Console.WriteLine("B.MyY: " + b.MyY().ToString());


The output of the application is:

A.MyX: 42

B.MyX: 30

A.MyY: 126

B.MyY: 126

Therefore the function MyA of class A was used and not the new one of class B. This is the correct behavior but it is also a source for errors. Therefore if you implement classes by using inheritance you have to take respect of this behavior and you may think twice when you hide a function by implementing a new function with the same name.


Even if C# does not allow multiple inheritance, you may have situations where a function exists in different classes with different implementations. In such situations it is not always easy to see and know the behavior of a function call. Therefore you have to be very carefully whenever you use inheritance and function hiding.

Dieser Beitrag wurde unter .NET, C# abgelegt und mit , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit deinem Abmelden /  Ändern )


Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s