Object-Oriented Programming

By Global Code Factory

Updated on:

Object-Oriented Programming Basics in C#

It is one of the fundamental ideas in modern software development. It facilitates the creation of scalable, manageable, and reusable code in C#. Developers widely use these ideas to create enterprise-level software. In the long run, gaining an understanding of this idea can help you advance professionally and develop your skills in coding.

In essence, the goal of object-oriented programming, or OOP, is to build software based on real-world entities. Classes and objects are the C# representations of these entities. An object is a real instance, whereas a class defines the blueprint. As a result, this approach facilitates code extension and maintenance.

Why Learn Object-Oriented Programming in C#?

In fact, Gaining knowledge of C# object-oriented programming gives the foundation for working on complex applications. It facilitates team collaboration as well. Moreover, OOP promotes modular architecture and boosts efficiency.

Transitioning from procedural to object-oriented design helps you write cleaner and more logical code. Additionally, it saves development time by enabling the reuse of pre-existing components.

Core Principles of Object-Oriented Programming in C# 

There are four primary OOP principles that C# adheres to. Each is essential to the design of the application.

1. Encapsulation

Encapsulation is the process of hiding the internal details of an object. By using access modifiers like private, public, and protected, you can manage who has access to its data.

Example of Encapsulation in C#:

public class BankAccount
{
    private decimal balance;

    public void Deposit(decimal amount)
    {
        if (amount > 0)
            balance += amount;
    }

    public decimal GetBalance()
    {
        return balance;
    }
}

The balance field is private in this case. Control over how the balance is updated is ensured by the fact that other code cannot directly alter it.

2. Inheritance

Inheritance allows a class to acquire properties and methods from another class. For this reason, it promotes reusability.

public class Vehicle
{
    public void Drive() { Console.WriteLine("Vehicle is driving"); }
}

public class Car : Vehicle
{
    public void Honk() { Console.WriteLine("Car is honking"); }
}

3. Polymorphism

One interface or method name can be used for multiple types thanks to polymorphism.

Example of Polymorphism in C#:

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Animal makes a sound");
    }
}

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

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

In this case, the Speak method behaves differently depending on the object type.

4. Abstraction

The goal of abstraction is to hiding implementation and reveal only the information that is required.

Example of Abstraction in C#:

public abstract class Payment
{
    public abstract void ProcessPayment();
}

public class CreditCardPayment : Payment
{
    public override void ProcessPayment()
    {
        Console.WriteLine("Processing credit card payment");
    }
}

public class PayPalPayment : Payment
{
    public override void ProcessPayment()
    {
        Console.WriteLine("Processing PayPal payment");
    }
}

Full Example: All Object-Oriented Programming Principles Together

In the following example, a single program incorporates abstraction, polymorphism, inheritance, and encapsulation.

public abstract class Employee // Abstraction
{
    public string Name { get; set; }
    public abstract void CalculateSalary();
}

public class Developer : Employee // Inheritance
{
    private decimal baseSalary; // Encapsulation

    public Developer(string name, decimal salary)
    {
        Name = name;
        baseSalary = salary;
    }

    public override void CalculateSalary() // Polymorphism
    {
        Console.WriteLine($"{Name}'s salary is {baseSalary + 2000}");
    }
}

public class Manager : Employee
{
    private decimal baseSalary; // Encapsulation

    public Manager(string name, decimal salary)
    {
        Name = name;
        baseSalary = salary;
    }

    public override void CalculateSalary() // Polymorphism
    {
        Console.WriteLine($"{Name}'s salary is {baseSalary + 5000}");
    }
}

This code makes use of polymorphism in the CalculateSalary method, encapsulation for salaries, inheritance for Developer and Manager, and abstraction with the Employee class.

Anyone hoping to work in software development must learn object-oriented programming in C#. With this knowledge, we can produce high-caliber, maintainable apps by comprehending abstraction, polymorphism, inheritance, and encapsulation. You will soon be able to produce effective, expert-level C# code if you begin practicing today.

Leave a Comment