11 Code Excercises on Inheritance in C# .NET

Introduction

This article explains the various behaviors of inheritance in OOP in various cases. Inheritance is creating classes that inherit certain aspects from parent classes. If you are not familiar with object-oriented programming, please read A Complete Guide To Object Oriented Programming In C#.

Objective

To understand inheritance and its behavior in various cases and to understand keywords like virtual, override, new and abstract.

CASE 1: What will happen when the virtual keyword is used with a method without implementation? For example:

class A  
{  
    public virtual void Show();  
}

RESULT

Error 1 'ConsoleApplication.A.Show()' must declare a body because it is not marked abstract, extern, or partial

CASE 2: What will happen when a method is used without implementation? For example:

class A  
{  
    public void Show();  
}
RESULT

Error 1 'ConsoleApplication.A.Show()' must declare a body because it is not marked abstract, extern, or partial

CASE 3: What will be the output of the C# .NET code snippet given below in which the base class method is overridden by a derived class using the override keyword?

class Program  
{  
    static void Main(string[] args)  
    {  
        A obj = new B();  
        obj.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }  
}  
class B : A  
{  
    public override void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}
RESULT

Overridding in C#

CASE 4: What will be the output of the C# .NET code snippet given below Where the base class method is overridden by a derived class using the new keyword?

class Program  
{  
    static void Main(string[] args)  
    {  
        A obj = new B();  
        obj.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }  
}  
class B : A  
{  
    public new void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}
RESULT

Overridding in C#

CASE 5: What will be the output of the C# .NET code snippet given below?

class Program  
{  
    static void Main(string[] args)  
    {  
        A obj = new B();  
        obj.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }  
}  
class B : A  
{  
    public void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}

RESULT

Warning 1 'ConsoleApplication.B.Show()' hides inherited member 'ConsoleApplication.A.Show()'. To make the current member override that implementation, add the override keyword. Otherwise, add the new keyword.

Output:

A.Show()

CASE 6: What happens when the abstract method is used with a non-abstract class?

class A  
{  
    public abstract void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }  
}
RESULT

Error 1 'ConsoleApplication.A.Show()' cannot declare a body because it is marked abstract
Error 2 'ConsoleApplication.A.Show()' is abstract but it is contained in non-abstract class 'ConsoleApplication.A'

CASE 7: What will be the output of the C# .NET code snippet given below?

class Program  
{  
    static void Main(string[] args)  
    {  
        C c = new C();  
        A a = new A();  
        a = c;  
        a.Show();  
        c.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }        
}  
class B : A  
{  
    public new void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}  
class C : B  
{  
    public new void Show()  
    {  
        Console.WriteLine("C.Show()");  
    }  
}
RESULT



CASE 8: What will be the output of the C# .NET code snippet given below?
class Program  
{  
    static void Main(string[] args)  
    {  
        C c = new C();  
        A a = new A();  
        a = c;  
        a.Show();  
        c.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }       
}  
  
class B : A  
{  
    public override void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}  
class C : B  
{  
    public new void Show()  
    {  
        Console.WriteLine("C.Show()");  
    }  
}  

RESULT



CASE 9: What will be the output of the C# .NET code snippet given below?

class Program  
{  
    static void Main(string[] args)  
    {  
        C c = new C();  
        A a = new A();  
        a = c;  
        a.Show();  
        c.Show();  
        Console.ReadLine();  
    }  
}  
class A  
{  
    public virtual void Show()  
    {  
        Console.WriteLine("A.Show()");  
    }       
  
}  
class B : A  
{  
    public override void Show()  
    {  
        Console.WriteLine("B.Show()");  
    }  
}  
class C : B  
{  
    public override void Show()  
    {  
        Console.WriteLine("C.Show()");  
    }  
}
RESULT



CASE 10: What will be the output of the C# .NET code snippet given below?

class Program  
{  
    static void Main(string[] args)  
    {  
        B b = new B(10);  
        Console.ReadLine();  
    }  
}  
class A  
{  
    int i;  
    public A(int j)  
    {  
        i = j;  
        Console.WriteLine("Base");  
    }  
}  
class B : A  
{  
    public B(int j)  
    {  
        Console.WriteLine("Derived");  
    }  
}
RESULT

Error 1 'ConsoleApplication.A' does not contain a constructor that takes 0 arguments

CASE 11: What will be the output of the C# .NET code snippet given below?
class Program  
{  
    static void Main(string[] args)  
    {  
        B b = new B(10);  
        Console.ReadLine();  
    }  
}  
class A  
{  
    int i;  
    public A(int j)  
    {  
        i = j;  
        Console.WriteLine("Base");  
    }  
}  
class B : A  
{  
    public B(int j)  
        : base(j)  
    {  
        Console.WriteLine("Derived");  
    }  
}

RESULT



Conclusion

Inheritance provides great reusability of the code. This post showed several code examples of inheritance in C# and .NET.

Next recommended readings:


Similar Articles