Object-Oriented Programming: Abstraction


Hides complexity by only exposing necessary details.
This allows users to interact with objects and systems without needing to understand all the underlying implementation details.
Real-world Example
TV Remote
A TV remote has several functions like turning the TV on or off, changing channels, increasing volume, etc.
As users, we only know that these functions will be executed by pressing the corresponding buttons on the remote.
What actually happens behind the scenes is hidden from us.
Benefits of Using Abstraction
Reduces Complexity
This principle only provides method signatures while hiding how those methods are implemented.
Enhances Security
Because we only provide the necessary details to call a method while hiding how the method is implemented.
Facilitates Development
Since we can make any internal system changes without affecting the users of the method.
Abstraction can be achieved in two ways:
Using abstract classes and abstract methods.
Using Interfaces.
Abstract Class and Abstract Method
Characteristics of an Abstract Class.
Cannot be instantiated directly.
Serves as a blueprint for other classes, meaning it can contain abstract methods (which must be implemented by its subclasses) and concrete methods (which have full implementations).
Characteristics of an Abstract Method
Declared in an abstract class without implementation.
Only has a method signature (method name, return type, and parameters) without implementation.
The actual implementation is left to the subclasses.
Example of an abstract class with all abstract methods.
public abstract class IBank
{
public abstract void ValidateCard();
public abstract void WithdrawMoney();
public abstract void CheckBalanace();
public abstract void BankTransfer();
public abstract void MiniStatement();
}
public class SBI : IBank
{
public override void BankTransfer()
{
Console.WriteLine("SBI Bank Bank Transfer");
}
public override void CheckBalanace()
{
Console.WriteLine("SBI Bank Check Balanace");
}
public override void MiniStatement()
{
Console.WriteLine("SBI Bank Mini Statement");
}
public override void ValidateCard()
{
Console.WriteLine("SBI Bank Validate Card");
}
public override void WithdrawMoney()
{
Console.WriteLine("SBI Bank Withdraw Money");
}
}
IBank sbi = new SBI();
sbi.ValidateCard();
sbi.WithdrawMoney();
sbi.CheckBalanace();
sbi.BankTransfer();
sbi.MiniStatement();
In the code above, we declare a class as an abstract class by adding the abstract modifier. We also declare all its methods as abstract methods, where no implementation is allowed (only the method body). However, when we create a subclass of IBank, in the example above, SBI, all methods from IBank must be implemented by adding the override keyword to each method in the child class.
We also need to pay attention during object initialization. On the left side or reference type, we set the object ‘sbi’ as IBank, the abstract class, while on the right side or object type, we set it as the SBI class. When we call a method, the compiler recognizes all these methods because they are in the abstract class, but during runtime, the implementation is determined based on the object type, in this case, SBI.
Example of an abstract class with abstract and concrete methods.
public abstract class Bird
{
public abstract void Fly();
public void LayEggs()
{
Console.WriteLine("Laying eggs");
}
}
public class Sparrow : Bird
{
public override void Fly()
{
Console.WriteLine("Sparrow is flying");
}
}
In the code example above, we declare some methods as abstract methods and others as concrete methods. This way, when we create a subclass or child class, we must override the Fly() method because in the parent class, the method is set as an abstract method (without implementation), while the LayEggs() method is not overridden because it is a concrete method (with implementation).
Interface.
An interface is an abstract type used to define a contract (or blueprint) for classes that implement it.
Let's look at the code example directly.
interface ISpeakable
{
void Speak(); // This method must be implemented by its child class.
}
interface IMoveable
{
void Move();
}
public class Dog : ISpeakable, IMoveable
{
public void Speak()
{
Console.WriteLine("Bark");
}
public void Move()
{
Console.WriteLine("Dog is running");
}
}
Dog dog = new Dog();
dog.Speak();
dog.Move();
Note: A class is said to implement an interface, not inherit from it.
In the code above, we can see that the Dog class implements two interfaces simultaneously, ISpeakable and IMoveable, which is allowed, unlike abstract classes that do not allow a child class to be derived from two classes simultaneously (C# allows a class to be derived from only one class).
The concept of interfaces is very popular and often encountered in complex projects.
Differences between Abstract Class and Interface
See the differences between abstract classes and interfaces in the table below.
Aspect | Abstract Class | Interface |
Nature | Represents "is-a" relationship | Represents "can do" relationship |
Method Implementation | Parent class can have abstract and concrete methods. | Only has method signatures (name, parameters, return type). |
Data Members | Can have fields, constructors, and properties | Can have properties, events, indexers, but not fields. |
Multiple Inheritance | Does not support multiple inheritance | Supports multiple inheritance |
Purpose | When several classes share the same methods but also need to add some unimplemented behaviors | When defining a contract that must be implemented by a class. |
Default Access Modifier | As a class, the default modifier is private. | Default modifier is set to public. |
We have finished discussing the concept of Abstraction in Object-Oriented Programming (OOP), thus completing our discussion on the four pillars of Object-Oriented Programming (OOP). Thank you!
Subscribe to my newsletter
Read articles from Kristiadhy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kristiadhy
Kristiadhy
Experienced Full Stack .NET developer with a proven track record of designing and implementing robust business applications. Proficient in using ASP.NET Core Web API, Blazor, and WinForms to deliver high-quality, efficient code and scalable solutions. Strong focus on implementing industry best practices for cleaner and scalable outcomes.