Encapsulation, Inheritance, and Polymorphism in C#

Introduction

Object-oriented programming (OOP) is a cornerstone of modern software development, enabling developers to create modular, reusable, and scalable code. Among the key principles of OOP are encapsulation, inheritance, and polymorphism. In this article, we'll explore these concepts in the context of C#, providing examples to illustrate their usage and benefits.

Encapsulation

Encapsulation is the practice of bundling data (fields) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some of the object's components, which is a means of preventing accidental interference and misuse of the data. Encapsulation is achieved by using access modifiers like private, protected, and public.

Example

public class Person
{
    // Fields are encapsulated within the class
    private string name;
    private int age;

    // Public methods provide controlled access to the fields
    public string GetName()
    {
        return name;
    }

    public void SetName(string name)
    {
        this.name = name;
    }

    public int GetAge()
    {
        return age;
    }

    public void SetAge(int age)
    {
        if (age > 0)
        {
            this.age = age;
        }
    }
}

C#

Copy

In this example, the name and age fields are private, meaning they cannot be accessed directly from outside the class. Instead, public methods (GetName, SetName, GetAge, and SetAge) are provided to get and set these values, allowing for controlled access and modification.

Inheritance

Inheritance allows a class to inherit the properties and methods of another class. This promotes code reusability and establishes a natural hierarchy between classes. In C#, the: symbol is used to denote inheritance.

Example

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
}

C#

Copy

Here, the Dog class inherits from the Animal class, meaning it has access to the Eat method. Additionally, the Dog class has its own method, Bark. This demonstrates how inheritance allows for the extension and reuse of existing code.

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. In C#, polymorphism can be achieved through method overriding and interfaces.

Method Overriding Example

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow");
    }
}

C#

Copy

In this example, the MakeSound method is overridden in the Dog and Cat classes to provide specific implementations. When the MakeSound method is called on a Dog or Cat object, the appropriate sound is made, demonstrating polymorphism.

Interface Example

public interface IShape
{
    void Draw();
}

public class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

public class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a square");
    }
}

C#

Copy

In this example, both Circle and Square classes implement the IShape interface, providing their own implementation of the Draw method. This allows for polymorphic behavior when interacting with objects through the IShape interface.

Conclusion

Encapsulation, inheritance, and polymorphism are fundamental concepts in object-oriented programming that enhance code modularity, reusability, and flexibility. Encapsulation protects data and maintains integrity, inheritance promotes code reuse and establishes relationships, and polymorphism allows for dynamic behavior based on object types. By leveraging these principles, developers can create robust and maintainable applications in C#.

Embracing these concepts will lead to cleaner, more efficient, and easier-to-maintain codebases, ultimately resulting in better software development practices.

Up Next
    Ebook Download
    View all
    Learn
    View all