C# -

Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit members (fields, properties, methods) from another class. This tutorial covers the basics of inheritance, types of inheritance, method overriding, interfaces, and best practices for using inheritance in C#.


1. Understanding Inheritance

Inheritance allows you to create a new class that is based on an existing class. The new class, called the derived class, inherits the members of the existing class, called the base class. Inheritance promotes code reuse and establishes a relationship between classes.

Example:
        
            namespace InheritanceExamples;

// Base class
class Animal
{
    public string Name { get; set; }

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating.");
    }
}

// Derived class
class Dog : Animal
{
    public string Breed { get; set; }

    public void Bark()
    {
        Console.WriteLine($"{Name} is barking.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog
        {
            Name = "Buddy",
            Breed = "Golden Retriever"
        };

        dog.Eat();
        dog.Bark();
    }
}
        
    

2. Types of Inheritance

C# supports single inheritance, where a class can inherit from only one base class. However, a class can implement multiple interfaces. Other forms of inheritance like multiple inheritance are not supported directly in C#, but similar behavior can be achieved using interfaces.


3. Method Overriding

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. To override a method, use the override keyword in the derived class, and the virtual keyword in the base class.

Example:
        
            namespace InheritanceExamples;

// Base class
class Animal
{
    public string Name { get; set; }

    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} is making a sound.");
    }
}

// Derived class
class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine($"{Name} is barking.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog { Name = "Buddy" };
        dog.MakeSound(); // Output: Buddy is barking.
    }
}
        
    

4. Interfaces vs. Inheritance

An interface defines a contract that a class must adhere to. Unlike inheritance, where a class inherits members from a base class, implementing an interface requires a class to provide implementations for all the methods and properties defined in the interface. Interfaces are used to define capabilities that can be added to any class, regardless of its place in the class hierarchy.

Example:
        
            namespace InheritanceExamples;

// Define an interface
interface IFlyable
{
    void Fly();
}

// Implement the interface in a class
class Bird : IFlyable
{
    public string Name { get; set; }

    public void Fly()
    {
        Console.WriteLine($"{Name} is flying.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Bird bird = new Bird { Name = "Sparrow" };
        bird.Fly(); // Output: Sparrow is flying.
    }
}
        
    

5. Sealed Classes and Methods

The sealed keyword is used to prevent a class from being inherited or a method from being overridden. Sealed classes and methods provide control over the inheritance hierarchy.

Example:
        
            namespace InheritanceExamples;

// Base class
class Animal
{
    public string Name { get; set; }

    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} is making a sound.");
    }
}

// Derived class
sealed class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine($"{Name} is barking.");
    }
}

// Attempting to derive from a sealed class will cause a compile-time error
// class Labrador : Dog { }

class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog { Name = "Buddy" };
        dog.MakeSound(); // Output: Buddy is barking.
    }
}
        
    

6. Best Practices for Inheritance


Conclusion

Inheritance is a powerful feature in C# that enables code reuse and the creation of a structured class hierarchy. By understanding the basics of inheritance, method overriding, interfaces, and best practices, you can effectively leverage inheritance to build robust and maintainable applications.