Mastering C# Part 7.2 Object Oriented Programming
Inheritance (Single inheritance, Multilevel, Hierarchical, Multiple inheritance, Overriding with virtual keyword, Base keyword, Abstract

Hi, I'm Nishant Banjade, a Software Engineer at Ellucian, committed to transforming education through technology. With expertise in developing CRM systems, plugins, workflows, APIs, and customized solutions for Higher Education, I bring a strong focus on innovation and efficiency. Currently, I'm honing my skills in full-stack software development, system design, data structures, algorithms, and Microsoft Dynamics 365.
I am a skilled software developer with expertise in C#/.NET, D365, JavaScript, TypeScript, ReactJS, SQL, and cloud technologies like AWS, along with experience in RabbitMQ, Docker, MUI, Tailwind, JIRA, and Git.
Inheritance in C#
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (derived class) to inherit properties, methods, and other members from another class (base class). It promotes code reuse, extensibility, and polymorphism.
Key Features of Inheritance in C#:
Base Class: The class whose members are inherited.
Derived Class: The class that inherits members of the base class.
Access Modifiers: Determine the visibility of the base class members in the derived class (
public,protected,private).is-aRelationship: Inheritance establishes an "is-a" relationship (e.g., Dog is a Animal).Single Inheritance: C# supports single inheritance directly, but multiple inheritance is achieved via interfaces.
Base Keyword: Used to access base class members or call its constructors.
Polymorphism: Enables overriding base class methods in the derived class.
Syntax
class BaseClass
{
// Members of the base class
}
class DerivedClass : BaseClass
{
// Members of the derived class
}
Example: Inheritance Basics
using System;
public class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} is barking.");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Name = "Buddy";
dog.Eat(); // Inherited from Animal
dog.Bark(); // Defined in Dog
}
}
Output:
Buddy is eating.
Buddy is barking.
Types of Inheritance in C#
Single Inheritance: One derived class inherits from a single base class.
class BaseClass { } class DerivedClass : BaseClass { }Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
class BaseClass { } class DerivedClass1 : BaseClass { } class DerivedClass2 : BaseClass { }Multilevel Inheritance: A class inherits from a derived class, forming a chain.
class GrandParent { } class Parent : GrandParent { } class Child : Parent { }Multiple Inheritance (via Interfaces): A class implements multiple interfaces.
interface IInterface1 { } interface IInterface2 { } class DerivedClass : IInterface1, IInterface2 { }
Access Modifiers in Inheritance
| Modifier | Base Class | Derived Class (Same Assembly) | Derived Class (Different Assembly) |
public | Yes | Yes | Yes |
protected | Yes | Yes | Yes |
private | Yes | No | No |
internal | Yes | Yes | No |
protected internal | Yes | Yes | Yes |
Overriding and the virtual Keyword
Base Class: Mark a method as
virtualto allow overriding.Derived Class: Use the
overridekeyword to modify behavior.
public class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal makes a sound.");
}
}
public class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks.");
}
}
class Program
{
static void Main()
{
Animal animal = new Animal();
animal.Sound();
Animal dog = new Dog(); // Polymorphism
dog.Sound();
}
}
Output:
Animal makes a sound.
Dog barks.
Base Keyword
The base keyword is used to:
Access Base Class Members: Invoke base class properties or methods.
Call Base Class Constructor: Pass parameters to the base class constructor.
Example:
public class Animal
{
public Animal(string name)
{
Console.WriteLine($"Animal: {name}");
}
}
public class Dog : Animal
{
public Dog(string name) : base(name)
{
Console.WriteLine($"Dog: {name}");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog("Buddy");
}
}
Output:
Animal: Buddy
Dog: Buddy
Abstract Classes
An abstract class provides a base for other classes and cannot be instantiated directly. It can include both abstract (unimplemented) and concrete (implemented) methods.
Example:
public abstract class Animal
{
public abstract void Sound(); // Abstract method
public void Eat()
{
Console.WriteLine("Animal is eating.");
}
}
public class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks.");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Eat();
dog.Sound();
}
}
Sealed Classes and Methods
Sealed Class: Cannot be inherited.
Sealed Method: Prevents further overriding of the method in derived classes.
Example:
public sealed class FinalClass
{
public void Display()
{
Console.WriteLine("This is a sealed class.");
}
}
Summary
Inheritance enables code reuse and extensibility.
Access Modifiers control the visibility of base class members.
Use
virtual,override, andabstractfor polymorphism.Sealed Classes prevent inheritance.
Use
baseto access base class members and constructors.