C# | Multilevel Inheritance
Last Updated :
11 Jul, 2025
Introduction:
In object-oriented programming, inheritance is the ability to create new classes that are derived from existing classes, known as base or parent classes. Inheritance allows the derived classes to inherit properties and methods of the base classes and to add new features or functionalities to the derived classes.
Inheritance is a powerful feature of object-oriented programming because it allows code reuse, increases code organization, and simplifies the development process. By using inheritance, developers can create new classes that inherit the common properties and behaviors of existing classes and then add new features or customize the inherited properties and behaviors.
There are several types of inheritance, including single inheritance, multiple inheritance, hierarchical inheritance, and multilevel inheritance. Each type of inheritance has its own advantages and disadvantages, and choosing the right type of inheritance depends on the specific needs of the application.
Overall, inheritance is a fundamental concept in object-oriented programming, and understanding its principles is essential for writing efficient and effective code
Advantages of inheritance in object-oriented programming:
- Code reuse: Inheritance allows developers to reuse code from existing classes, reducing the amount of new code that needs to be written. This makes the development process more efficient and faster.
- Enhances code organization: Inheritance helps to organize code by creating a hierarchical structure of classes. This makes it easier to understand and manage the codebase.
- Simplifies maintenance: By using inheritance, changes to the base class automatically apply to all the derived classes. This reduces the amount of time and effort required to maintain the codebase.
- Promotes extensibility: Inheritance allows developers to extend the functionality of existing classes by adding new properties and methods to the derived classes.
Disadvantages of inheritance in object-oriented programming:
- Inheritance can lead to tight coupling between classes, making the code more difficult to modify and maintain.
- Inheritance can lead to code duplication and redundancy, especially when multiple derived classes inherit from the same base class.
- Inheritance can make the code more complex and harder to understand, especially when there are many levels of inheritance.
- Inheritance can lead to the creation of large and complicated class hierarchies, which can be difficult to manage and may impact performance.
- Overall, inheritance is a powerful tool in object-oriented programming, but it should be used judiciously, with careful consideration of the specific needs of the application..
In C#, multilevel inheritance refers to the ability to create a derived class that inherits from a base class, and then create another derived class that inherits from the first derived class. This creates a hierarchical structure of classes, where each class inherits the properties and methods of the classes above it in the hierarchy.
Here is an example of multilevel inheritance in C#:
C#
using System;
public class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
public class Mammal : Animal
{
public void Walk()
{
Console.WriteLine("The mammal is walking.");
}
}
public class Dog : Mammal
{
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}
public class Program
{
public static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Walk();
myDog.Bark();
}
}
OutputThe animal is eating.
The mammal is walking.
The dog is barking.
In this example, the Animal class is the base class, the Mammal class is derived from Animal, and the Dog class is derived from Mammal. The Dog class inherits the Eat() method from the Animal class, the Walk() method from the Mammal class, and also has its own Bark() method.
When the Main() method is executed, it creates a new instance of the Dog class and calls its methods, including those inherited from the base and intermediate classes. The output of the program will be:
In the Multilevel inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class to other class. For example, three classes called A, B, and C, as shown in the below image, where class C is derived from class B and class B, is derived from class A. In this situation, each derived class inherit all the characteristics of its base classes. So class C inherits all the features of class A and B.
Example: Here, the derived class Rectangle is used as a base class to create the derived class called ColorRectangle. Due to inheritance the ColorRectangle inherit all the characteristics of Rectangle and Shape and add an extra field called rcolor, which contains the color of the rectangle.
This example also covers the concept of constructors in a derived class. As we know that a subclass inherits all the members (fields, methods) from its superclass but constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. As shown in the below example, base refers to a constructor in the closest base class. The base in ColorRectangle calls the constructor in Rectangle and the base in Rectangle class the constructor in Shape.
CSharp
// C# program to illustrate the
// concept of multilevel inheritance
using System;
class Shape {
double a_width;
double a_length;
// Default constructor
public Shape()
{
Width = Length = 0.0;
}
// Constructor for Shape
public Shape(double w, double l)
{
Width = w;
Length = l;
}
// Construct an object with
// equal length and width
public Shape(double y)
{
Width = Length = y;
}
// Properties for Length and Width
public double Width
{
get {
return a_width;
}
set {
a_width = value < 0 ? -value : value;
}
}
public double Length
{
get {
return a_length;
}
set {
a_length = value < 0 ? -value : value;
}
}
public void DisplayDim()
{
Console.WriteLine("Width and Length are "
+ Width + " and " + Length);
}
}
// A derived class of Shape
// for the rectangle.
class Rectangle : Shape {
string Style;
// A default constructor.
// This invokes the default
// constructor of Shape.
public Rectangle()
{
Style = "null";
}
// Constructor
public Rectangle(string s, double w, double l)
: base(w, l)
{
Style = s;
}
// Construct an square.
public Rectangle(double y)
: base(y)
{
Style = "square";
}
// Return area of rectangle.
public double Area()
{
return Width * Length;
}
// Display a rectangle's style.
public void DisplayStyle()
{
Console.WriteLine("Rectangle is " + Style);
}
}
// Inheriting Rectangle class
class ColorRectangle : Rectangle {
string rcolor;
// Constructor
public ColorRectangle(string c, string s,
double w, double l)
: base(s, w, l)
{
rcolor = c;
}
// Display the color.
public void DisplayColor()
{
Console.WriteLine("Color is " + rcolor);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
ColorRectangle r1 = new ColorRectangle("pink",
"Fibonacci rectangle", 2.0, 3.236);
ColorRectangle r2 = new ColorRectangle("black",
"Square", 4.0, 4.0);
Console.WriteLine("Details of r1: ");
r1.DisplayStyle();
r1.DisplayDim();
r1.DisplayColor();
Console.WriteLine("Area is " + r1.Area());
Console.WriteLine();
Console.WriteLine("Details of r2: ");
r2.DisplayStyle();
r2.DisplayDim();
r2.DisplayColor();
Console.WriteLine("Area is " + r2.Area());
}
}
Here are some recommended books for learning about inheritance in object-oriented programming:
- "Head First Object-Oriented Analysis and Design" by Brett McLaughlin, Gary Pollice, and David West: This book is a great resource for beginners who want to learn about object-oriented programming concepts, including inheritance, in a fun and interactive way.
- "Object-Oriented Programming in C++" by Robert Lafore: This book provides a comprehensive introduction to object-oriented programming in C++, including inheritance, polymorphism, and encapsulation.
- "Java: A Beginner's Guide" by Herbert Schildt: This book is a great resource for learning Java, including object-oriented programming concepts such as inheritance.
- "Python for Everybody: Exploring Data in Python 3" by Charles Severance: This book is a great resource for learning Python, including object-oriented programming concepts such as inheritance.
- "C# 8.0 and .NET Core 3.0 - Modern Cross-Platform Development" by Mark J. Price: This book is a comprehensive guide to learning C# and .NET Core 3.0, including object-oriented programming concepts such as inheritance.
- These books are great resources for both beginners and experienced developers who want to learn more about inheritance in object-oriented programming.
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers