Object-Oriented Programming (OOP) in C#

Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (also known as attributes or properties), and code in the form of methods. OOP promotes the organization of software into reusable, modular components. The four core principles of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism.

Encapsulation

Encapsulation refers to the bundling of data and methods that operate on the data into a single unit or class. It restricts access to the internal workings of an object, protecting it from unintended interference.

Example: Using Encapsulation in C#

class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public void Display()
    {
        Console.WriteLine("Name: " + Name);
    }
}

Person person = new Person();
person.Name = "Alice";
person.Display();  // Output: Name: Alice
        

In this example, name is a private field, and access to it is provided through the public Name property. This is an example of encapsulation, where the internal details are hidden from outside access.

Abstraction

Abstraction simplifies complex reality by modeling classes based on essential properties and behaviors. It hides the implementation details and shows only the functionality to the user.

Example: Using Abstraction in C#

abstract class Animal
{
    public abstract void MakeSound();
}

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

Animal myDog = new Dog();
myDog.MakeSound();  // Output: Bark
        

In this example, Animal is an abstract class with an abstract method MakeSound(). The Dog class inherits from Animal and provides the specific implementation of MakeSound().

Inheritance

Inheritance allows a class to inherit fields and methods from another class. It promotes code reusability and the creation of a hierarchical class structure.

Example: Using Inheritance in C#

class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }

    public void ShowDetails()
    {
        Console.WriteLine($"Vehicle: {Make} {Model}");
    }
}

class Car : Vehicle
{
    public int Doors { get; set; }
}

Car myCar = new Car { Make = "Toyota", Model = "Camry", Doors = 4 };
myCar.ShowDetails();  // Output: Vehicle: Toyota Camry
        

In this example, the Car class inherits from the Vehicle class, and myCar has access to both Vehicle and Car properties.

Polymorphism

Polymorphism allows one interface to be used for different data types. In C#, polymorphism can be achieved through method overriding and interface implementation.

Example: Using Polymorphism in C#

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape.");
    }
}

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

Shape shape = new Circle();
shape.Draw();  // Output: Drawing a circle.
        

In this example, the Draw() method is overridden in the Circle class, demonstrating polymorphism. Even though the reference is of type Shape, it calls the Circle implementation.

Key Points to Remember

  • Encapsulation hides the internal state of an object and exposes only the necessary details.
  • Abstraction focuses on essential characteristics, hiding implementation details.
  • Inheritance allows one class to inherit members from another, promoting code reusability.
  • Polymorphism enables one method or interface to work with different types, enhancing flexibility.