Inheritance, Abstraction, Polymorphism, and Encapsulation

These four concepts are the cornerstones of Object-Oriented Programming (OOP) and are heavily used in C#.

Inheritance

Inheritance is a mechanism that allows one class to inherit the fields and methods of another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

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

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

A Dog is an Animal, so it inherits the Eat method from Animal. You can call the Eat method on a Dog object:

Dog myDog = new Dog();
myDog.Eat();  // Outputs "Eating..."
myDog.Bark();  // Outputs "Barking..."

Abstraction

Abstraction is a process of hiding the implementation details and showing only the functionality to the user. In other words, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.

public abstract class Shape
{
    public abstract double Area();
}

public class Rectangle : Shape
{
    public double Height { get; set; }
    public double Width { get; set; }
    public override double Area() {
        return Height * Width;
    }
}

Reference: https://thecodeprogram.com/c--abstraction-explanation-with-examples

Polymorphism

Polymorphism means "many forms". It allows you to use an entity in multiple forms. In C#, you can achieve polymorphism by using inheritance and abstract classes or interfaces.

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

public class Pig : Animal
{
    public override void MakeSound() {
        Console.WriteLine("The pig says: wee wee");
    }
}


Inheritance, Abstraction, Polymorphism, and Encapsulation

These four concepts are the cornerstones of Object-Oriented Programming (OOP) and are heavily used in C#.

Inheritance

Inheritance is a mechanism that allows one class to inherit the fields and methods of another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

csharpCopy codepublic class Animal  // Base class
{
    public void Eat() {
        Console.WriteLine("Eating...");
    }
}

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

A Dog is an Animal, so it inherits the Eat method from Animal. You can call the Eat method on a Dog object:

csharpCopy codeDog myDog = new Dog();
myDog.Eat();  // Outputs "Eating..."
myDog.Bark();  // Outputs "Barking..."

Abstraction

Abstraction is a process of hiding the implementation details and showing only the functionality to the user. In other words, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.

csharpCopy codepublic abstract class Shape
{
    public abstract double Area();
}

public class Rectangle : Shape
{
    public double Height { get; set; }
    public double Width { get; set; }
    public override double Area() {
        return Height * Width;
    }
}

Polymorphism

Polymorphism means "many forms". It allows you to use an entity in multiple forms. In C#, you can achieve polymorphism by using inheritance and abstract classes or interfaces.

csharpCopy codepublic class Animal
{
    public virtual void MakeSound() {
        Console.WriteLine("The animal makes a sound");
    }
}

public class Pig : Animal
{
    public override void MakeSound() {
        Console.WriteLine("The pig says: wee wee");
    }
}

Encapsulation

Encapsulation is a mechanism that binds together the code and the data it manipulates and keeps both safe from external interference and misuse. Data encapsulation is a process of hiding the data of a class from external entities.

public class Employee
{
    private int employeeId;  // private variable

    // public method to set value of private variable
    public void SetId(int id)
    {
        employeeId = id;
    }

    // public method to get value of private variable
    public int GetId()
    {
        return employeeId;
    }
}

Complete and Continue