Object-Oriented Programming (OOP) stands as a foundational paradigm that revolutionizes how programmers conceive and construct software applications. In order to understand what it is and how to use it while developing C# applications, we need to talk about the four pillars.

The Pillars of OOP in .NET

Encapsulation

Encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, known as an object. In .NET, this is achieved through classes and structs. Encapsulation hides the internal state of objects, exposing only what is necessary through public methods and properties, thus safeguarding the integrity of the data.

Example:

public class User
{
    private string _username;
    private string _password;

    public User(string username, string password)
    {
        _username = username;
        _password = password;
    }

    public void ChangePassword(string newPassword)
    {
        // Add logic to change password
    }
}

Inheritance

Inheritance is a mechanism wherein a new class, known as a derived class, inherits attributes and behaviors from an existing class, referred to as the base class. This principle facilitates code reuse and the creation of a hierarchical classification of classes. .NET supports single inheritance, along with interfaces to implement multiple inheritances.

Example:

public class AdminUser : User
{
    public AdminUser(string username, string password)
        : base(username, password)
    {
    }

    public void ResetUserPassword(User user)
    {
        // Add logic to reset the password for a user
    }
}

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. In .NET, polymorphism is achieved through method overriding (using the virtual and override keywords) and method overloading.

Example:

public abstract class Shape
{
    public abstract void Draw();
}

public class Circle : Shape
{
    public override void Draw()
    {
        // Draw a circle
    }
}

public class Rectangle : Shape
{
    public override void Draw()
    {
        // Draw a rectangle
    }
}

Another way to achieve polymorphism is through interfaces. Interfaces in .NET define a contract, a set of methods and properties that implementing classes must provide.

public interface IMovable
{
    void Move();
}

public interface IFlyable
{
    void Fly();
}

public class Drone : IMovable, IFlyable
{
    public void Move()
    {
        Console.WriteLine("Drone is moving.");
    }

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

In this example, the Drone class implements two interfaces, IMovable and IFlyable, each providing a different capability. This approach enables the Drone class to inherit multiple behaviors without the drawbacks of multiple inheritance from classes and also it is able to behave either as IFlyable or IMovable.

Abstraction

Abstraction is the concept of hiding complex realities while exposing only the necessary parts. It is closely related to encapsulation and is achieved in .NET through abstract classes and interfaces. This allows for defining contracts for what a set of derived classes should implement.

Example:

public interface IVehicle
{
    void Start();
}

public class Car : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Turning the ignition key to start the car.");
    }
}

public class Motorcycle : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Pressing the start button to start the motorcycle.");
    }
}

Conclusion

Embracing Object-Oriented Programming in .NET not only enhances the robustness and flexibility of software applications but also elevates the developer’s capability to think abstractly and architect software that stands the test of time. By adhering to the principles of encapsulation, inheritance, polymorphism, and abstraction, developers can craft efficient, scalable, and maintainable applications.

If you love learning new stuff and want to support me, consider buying a course like Getting Started: Microservices Architecture or by using this link

Leave a comment

Trending