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:
- 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 happen 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.
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.
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?
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.