Inheritance in C# Programming


Introduction

Inheritance is one of the core concepts of object-oriented programming (OOP) in C#. It allows a class (derived class) to inherit properties and methods from another class (base class). Inheritance promotes code reuse and establishes a relationship between the base class and the derived class.

This tutorial will cover the following topics:

  • Base and derived classes
  • Method overriding
  • Sealed classes and methods

Base and Derived Classes

In C#, inheritance allows a derived class to inherit members (fields, properties, methods) from a base class. The derived class can add its own members or override the base class members to modify their behavior.

Syntax for Base and Derived Classes

    class BaseClass
    {
        // Base class members
    }     class DerivedClass : BaseClass
    {
        // Derived class members
    }
        

Example: Base and Derived Classes

Let's define a base class Animal and a derived class Dog that inherits from Animal.

    using System;     class Animal
    {
        public void Eat()
        {
            Console.WriteLine("Animal is eating");
        }
    }     class Dog : Animal
    {
        public void Bark()
        {
            Console.WriteLine("Dog is barking");
        }
    }     class Program
    {
        static void Main()
        {
            Dog dog = new Dog();
            dog.Eat();  // Inherited method from Animal class
            dog.Bark(); // Method from Dog class
        }
    }
        

Output:

    Animal is eating
    Dog is barking
        

Method Overriding

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in the base class. In order to override a method, the base class method must be marked as virtual and the derived class method must be marked as override.

Syntax for Method Overriding

    class BaseClass
    {
        public virtual void Method()
        {
            // Base class method
        }
    }     class DerivedClass : BaseClass
    {
        public override void Method()
        {
            // Overridden method
        }
    }
        

Example: Method Overriding

In this example, the Animal class has a method MakeSound, and the Dog class overrides that method to provide a specific implementation.

    using System;     class Animal
    {
        // Virtual method
        public virtual void MakeSound()
        {
            Console.WriteLine("Animal makes a sound");
        }
    }     class Dog : Animal
    {
        // Overridden method
        public override void MakeSound()
        {
            Console.WriteLine("Dog barks");
        }
    }     class Program
    {
        static void Main()
        {
            Animal animal = new Animal();
            animal.MakeSound();  // Output: Animal makes a sound             Dog dog = new Dog();
            dog.MakeSound();     // Output: Dog barks
        }
    }
        

Output:

    Animal makes a sound
    Dog barks
        

Sealed Classes and Methods

In C#, you can prevent a class from being inherited or a method from being overridden using the sealed keyword.

  • Sealed Class: A class that cannot be inherited.
  • Sealed Method: A method that cannot be overridden in a derived class.

Sealed Class

When a class is marked as sealed, it cannot be inherited by any other class. This is useful when you want to prevent further specialization of a class.

Example: Sealed Class

    using System;     sealed class Animal
    {
        public void Eat()
        {
            Console.WriteLine("Animal is eating");
        }
    }     // The following code will cause an error because the Animal class is sealed
    // class Dog : Animal { }     class Program
    {
        static void Main()
        {
            Animal animal = new Animal();
            animal.Eat();
        }
    }
        

Output:

    Animal is eating
        

Sealed Method

You can also use the sealed keyword with methods to prevent further overriding of the method in derived classes. The method must be overridden in a derived class before it can be sealed.

Example: Sealed Method

    using System;     class Animal
    {
        public virtual void MakeSound()
        {
            Console.WriteLine("Animal makes a sound");
        }
    }     class Dog : Animal
    {
        // Overriding the method
        public override void MakeSound()
        {
            Console.WriteLine("Dog barks");
        }
    }     class Bulldog : Dog
    {
        // Sealing the overridden method to prevent further overriding
        public sealed override void MakeSound()
        {
            Console.WriteLine("Bulldog barks loudly");
        }
    }     class Poodle : Bulldog
    {
        // The following code will cause an error because the method is sealed
        // public override void MakeSound() { Console.WriteLine("Poodle barks softly"); }
    }     class Program
    {
        static void Main()
        {
            Animal animal = new Animal();
            animal.MakeSound();  // Output: Animal makes a sound             Dog dog = new Dog();
            dog.MakeSound();     // Output: Dog barks             Bulldog bulldog = new Bulldog();
            bulldog.MakeSound(); // Output: Bulldog barks loudly
        }
    }
        

Output:

    Animal makes a sound
    Dog barks
    Bulldog barks loudly
        

Conclusion

In this tutorial, we've explored inheritance in C# programming, including:

  • Base and derived classes for establishing relationships between classes.
  • Method overriding for modifying the behavior of inherited methods.
  • Sealed classes and methods for preventing further inheritance or overriding when necessary.

Inheritance is a powerful feature of object-oriented programming, helping to create hierarchical relationships and enabling code reuse. By using method overriding and sealed classes or methods appropriately, you can create flexible and maintainable code in your applications.




Advertisement