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();
}
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()");
}
}

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()");
}
}

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?





Rohini UppinPosted Jun 17, 2025, 4:11 PM
Good coverage of cases
Gopal chauhanPosted Aug 9, 2022, 6:21 AM
Very good article,easy to understand
AishwaryaPosted Apr 22, 2021, 12:06 PM
Can somebody explain case 10 and case 11
avishek malviyaPosted Oct 20, 2018, 12:48 PM
Owesome code.
neeraj kathuriaPosted Aug 21, 2016, 3:25 PM
Can somebody please explain CASE 8 in detail
Munesh SharmaPosted Jun 15, 2016, 11:59 AM
nice
Hemant BharadwajPosted May 29, 2016, 4:29 PM
Good Work .. Really Helpful...Thanks
Mahesh DurgameditedPosted Mar 6, 2013, 5:45 AMEdited Mar 6, 2013, 5:45 AM
Nice Article
islam elbazPosted Jan 20, 2013, 1:54 PM
Good and Simple, Brilliant ...
Anurag SarkarPosted Jan 15, 2013, 7:47 AM
nice.
GanesanPosted Jan 15, 2013, 5:27 AM
Amazing..:)
Anil KumarPosted Jan 15, 2013, 1:10 AM
good work :)