Playing With Inheritance in C#.NET

Introduction

This article explains the various behaviors of inheritance in various cases. Inheritance is the ability to create classes which inherits certain aspects from parent classes.

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:

CODE

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:

CODE

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?

CODE

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

B.Show()

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?

CODE

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

A.Show()

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

CODE

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 happen when the abstract method is used with a non-abstract class?

CODE

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?

CODE

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

A.Show()
C.Show()

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

CODE

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

B.Show()
C.Show()

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

CODE

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

C.Show()
C.Show()

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

CODE

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?

CODE

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

Base
Derived

Conclusion

Inheritance provides great reusability of the code.

Up Next
    Ebook Download
    View all
    Learn
    View all