Inheritance in C#.NET

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.

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:

  1. class A  
  2. {  
  3.     public virtual void Show();  
  4. }  
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:
  1. class A  
  2. {  
  3.     public void Show();  
  4. }  
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?

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         A obj = new B();  
  6.         obj.Show();  
  7.         Console.ReadLine();  
  8.     }  
  9. }  
  10. class A  
  11. {  
  12.     public virtual void Show()  
  13.     {  
  14.         Console.WriteLine("A.Show()");  
  15.     }  
  16. }  
  17. class B : A  
  18. {  
  19.     public override void Show()  
  20.     {  
  21.         Console.WriteLine("B.Show()");  
  22.     }  
  23. }  
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?

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         A obj = new B();  
  6.         obj.Show();  
  7.         Console.ReadLine();  
  8.     }  
  9. }  
  10. class A  
  11. {  
  12.     public virtual void Show()  
  13.     {  
  14.         Console.WriteLine("A.Show()");  
  15.     }  
  16. }  
  17. class B : A  
  18. {  
  19.     public new void Show()  
  20.     {  
  21.         Console.WriteLine("B.Show()");  
  22.     }  
  23. }  
RESULT

Overridding in C#

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

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         A obj = new B();  
  6.         obj.Show();  
  7.         Console.ReadLine();  
  8.     }  
  9. }  
  10. class A  
  11. {  
  12.     public virtual void Show()  
  13.     {  
  14.         Console.WriteLine("A.Show()");  
  15.     }  
  16. }  
  17. class B : A  
  18. {  
  19.     public void Show()  
  20.     {  
  21.         Console.WriteLine("B.Show()");  
  22.     }  
  23. }  
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?
  1. class A  
  2. {  
  3.     public abstract void Show()  
  4.     {  
  5.         Console.WriteLine("A.Show()");  
  6.     }  
  7. }  
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?

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         C c = new C();  
  6.         A a = new A();  
  7.         a = c;  
  8.         a.Show();  
  9.         c.Show();  
  10.         Console.ReadLine();  
  11.     }  
  12. }  
  13. class A  
  14. {  
  15.     public virtual void Show()  
  16.     {  
  17.         Console.WriteLine("A.Show()");  
  18.     }        
  19. }  
  20. class B : A  
  21. {  
  22.     public new void Show()  
  23.     {  
  24.         Console.WriteLine("B.Show()");  
  25.     }  
  26. }  
  27. class C : B  
  28. {  
  29.     public new void Show()  
  30.     {  
  31.         Console.WriteLine("C.Show()");  
  32.     }  
  33. }  
RESULT



CASE 8: What will be the output of the C# .NET code snippet given below?
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         C c = new C();  
  6.         A a = new A();  
  7.         a = c;  
  8.         a.Show();  
  9.         c.Show();  
  10.         Console.ReadLine();  
  11.     }  
  12. }  
  13. class A  
  14. {  
  15.     public virtual void Show()  
  16.     {  
  17.         Console.WriteLine("A.Show()");  
  18.     }       
  19. }  
  20.   
  21. class B : A  
  22. {  
  23.     public override void Show()  
  24.     {  
  25.         Console.WriteLine("B.Show()");  
  26.     }  
  27. }  
  28. class C : B  
  29. {  
  30.     public new void Show()  
  31.     {  
  32.         Console.WriteLine("C.Show()");  
  33.     }  
  34. }  
RESULT



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

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         C c = new C();  
  6.         A a = new A();  
  7.         a = c;  
  8.         a.Show();  
  9.         c.Show();  
  10.         Console.ReadLine();  
  11.     }  
  12. }  
  13. class A  
  14. {  
  15.     public virtual void Show()  
  16.     {  
  17.         Console.WriteLine("A.Show()");  
  18.     }       
  19.   
  20. }  
  21. class B : A  
  22. {  
  23.     public override void Show()  
  24.     {  
  25.         Console.WriteLine("B.Show()");  
  26.     }  
  27. }  
  28. class C : B  
  29. {  
  30.     public override void Show()  
  31.     {  
  32.         Console.WriteLine("C.Show()");  
  33.     }  
  34. }  
RESULT



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

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         B b = new B(10);  
  6.         Console.ReadLine();  
  7.     }  
  8. }  
  9. class A  
  10. {  
  11.     int i;  
  12.     public A(int j)  
  13.     {  
  14.         i = j;  
  15.         Console.WriteLine("Base");  
  16.     }  
  17. }  
  18. class B : A  
  19. {  
  20.     public B(int j)  
  21.     {  
  22.         Console.WriteLine("Derived");  
  23.     }  
  24. }  
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?
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         B b = new B(10);  
  6.         Console.ReadLine();  
  7.     }  
  8. }  
  9. class A  
  10. {  
  11.     int i;  
  12.     public A(int j)  
  13.     {  
  14.         i = j;  
  15.         Console.WriteLine("Base");  
  16.     }  
  17. }  
  18. class B : A  
  19. {  
  20.     public B(int j)  
  21.         : base(j)  
  22.     {  
  23.         Console.WriteLine("Derived");  
  24.     }  
  25. }  
RESULT



Conclusion

Inheritance provides great reusability of the code. 

Further recommended readings on C# inheritance 
 
Here are more articles on inheritance and object oriented programming in C#.
 
Types of Inheritance in C# 
In this article, we will learn about C# inheritance and types of inheritance in C# and .NET with code examples.

Overview of Inheritance in C#
In this article, you will learn about inheritance in C#. Inheritance is one of the major pillars of object-oriented programming. Here is an overview of inheritance.

Inheritance with Example in C#
Here are some example of inheritance using C#.
 
Learn all about Inheritance in C#
In this article, you will learn about Inheritance in C#.
 
Playing with inheritance in C#
This article explains the various behaviors of inheritance in various cases.
 
Inheritance examples in C#
This article explains inheritance with examples in C#.
 
Multiple Inheritance in C#
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, will a Class fully represent an object?
 
Multiple Inheritance in C# using Interfaces
This is the simple mathematical operation program demonstrating how multiple inheritance can be achieved in C# using Interface Concept.
 
Inheritance in C#
Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.