Skip to main content

Command Palette

Search for a command to run...

Mastering C# Part 7.2 Object Oriented Programming

Inheritance (Single inheritance, Multilevel, Hierarchical, Multiple inheritance, Overriding with virtual keyword, Base keyword, Abstract

Updated
4 min read
Mastering C# Part 7.2 Object Oriented Programming
N

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#:

  1. Base Class: The class whose members are inherited.

  2. Derived Class: The class that inherits members of the base class.

  3. Access Modifiers: Determine the visibility of the base class members in the derived class (public, protected, private).

  4. is-a Relationship: Inheritance establishes an "is-a" relationship (e.g., Dog is a Animal).

  5. Single Inheritance: C# supports single inheritance directly, but multiple inheritance is achieved via interfaces.

  6. Base Keyword: Used to access base class members or call its constructors.

  7. 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#

  1. Single Inheritance: One derived class inherits from a single base class.

     class BaseClass { }
     class DerivedClass : BaseClass { }
    
  2. Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

     class BaseClass { }
     class DerivedClass1 : BaseClass { }
     class DerivedClass2 : BaseClass { }
    
  3. Multilevel Inheritance: A class inherits from a derived class, forming a chain.

     class GrandParent { }
     class Parent : GrandParent { }
     class Child : Parent { }
    
  4. Multiple Inheritance (via Interfaces): A class implements multiple interfaces.

     interface IInterface1 { }
     interface IInterface2 { }
     class DerivedClass : IInterface1, IInterface2 { }
    

Access Modifiers in Inheritance

ModifierBase ClassDerived Class (Same Assembly)Derived Class (Different Assembly)
publicYesYesYes
protectedYesYesYes
privateYesNoNo
internalYesYesNo
protected internalYesYesYes

Overriding and the virtual Keyword

  • Base Class: Mark a method as virtual to allow overriding.

  • Derived Class: Use the override keyword 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:

  1. Access Base Class Members: Invoke base class properties or methods.

  2. 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, and abstract for polymorphism.

  • Sealed Classes prevent inheritance.

  • Use base to access base class members and constructors.