Abstraction - Hides Complexity (C#)

Patel Dhiren
2 min readMay 25, 2023

--

Abstraction is another fundamental concept in object-oriented programming (OOP) that allows you to Hiding the internal implementation and show only the essential information to the users. Abstraction is a natural extension of Encapsulation. Abstraction aims to hide complexity from the users and show them only the relevant information.

Let’s continue with the example of a laptop to illustrate abstraction in C#:

abstract class Laptop
{
public string Brand { get; set; }
public string Model { get; set; }

public abstract void PowerOn();
public abstract void PowerOff();
public abstract void DisplayDetails();
}

class DellXPS13 : Laptop
{
public override void PowerOn()
{
Console.WriteLine("Dell XPS 13 is powering on...");
}

public override void PowerOff()
{
Console.WriteLine("Dell XPS 13 is powering off...");
}

public override void DisplayDetails()
{
Console.WriteLine($"Brand: {Brand}, Model: {Model}");
// Additional details specific to Dell XPS 13
}
}

class HPSpectre : Laptop
{
public override void PowerOn()
{
Console.WriteLine("HP Spectre is powering on...");
}

public override void PowerOff()
{
Console.WriteLine("HP Spectre is powering off...");
}

public override void DisplayDetails()
{
Console.WriteLine($"Brand: {Brand}, Model: {Model}");
// Additional details specific to HP Spectre
}
}

class Program
{
static void Main(string[] args)
{
Laptop dellXPS13 = new DellXPS13();
dellXPS13.Brand = "Dell";
dellXPS13.Model = "XPS 13";
dellXPS13.PowerOn();
dellXPS13.DisplayDetails();
dellXPS13.PowerOff();

Laptop hpSpectre = new HPSpectre();
hpSpectre.Brand = "HP";
hpSpectre.Model = "Spectre x360";
hpSpectre.PowerOn();
hpSpectre.DisplayDetails();
hpSpectre.PowerOff();
}
}

In this updated example, we have an abstract class Laptop that defines the common properties and abstract methods for a laptop. The PowerOn(), PowerOff(), and DisplayDetails() methods are declared as abstract, which means they are defined in the derived classes but not in the base class.

Two derived classes DellXPS13 and HPSpectre inherit from the Laptop class and provide their own implementations for the abstract methods.

In the Main method, we create instances of the derived classes (DellXPS13 and HPSpectre) and demonstrate the usage of the abstract methods. Each laptop is powered on, its details are displayed, and then it is powered off.

By using abstraction, we can define a common interface for laptops through the abstract class Laptop. The derived classes provide their specific implementations for the abstract methods, ensuring that each derived class adheres to the interface defined by the base class. This promotes code reusability and allows us to work with laptops at a higher level of abstraction, focusing on the essential behaviors rather than the implementation details.

Thank you! I’m glad to hear that you’re enjoying the learning process. If you have any more questions or need further assistance, feel free to ask. Happy learning to you too!

--

--