DEV Community

Cover image for Concepts Of Object-Oriented Programming -Part 3(Abstraction)
Elegberun Olugbenga
Elegberun Olugbenga

Posted on

Concepts Of Object-Oriented Programming -Part 3(Abstraction)

Welcome to the part 3 of our exploration into Object Oriented Programming. If you have not read part two check it out encapsulation. Part 1-Inheritance. In this article we will be talking about abstraction in Object Oriented Programming.
A lot of people often confuse encapsulation and abstraction and it is understandable because they kind of have similar jobs but they are not the same.Encapsulation refers to the full application and choosing which (information in the application to show) while Abstraction refers to the details(implementation hiding) choosing whether to show the inner workings of what you chose to display .
What we do in abstraction is define what the method will do but the actual implementation is done in another class. Using this example to illustrate.

Encapsulation and Abstraction Image

Abstraction vs Encapsulation

When you plug your phone to a socket in your house you do not see the inner workings of the socket but just the surface of the socket. Abstraction is hiding the detailed working of the sockets and encapsulation is choosing whether to even show the socket at all.
Abstraction is done to hide the concrete implementation whilst keeping the surface visible. But the whole socket is one entity encapsulated together.I hope this is clear.
There are several ways to achieve abstraction but I will talk about two ways. Interfaces and Abstract Classes.

Interfaces

Interfaces are created with the interface keyword. According to wikipedia an interface is a shared boundary across which two or more separate components of a computer system exchange information. It is just a boundary an intermediary to which we can communicate with another system.Look at the figure below.

Interface

Interfaces in C#

Lets create an interface called Register. By convention interfaces start with the capital letter I
Example:

 public interface IRegister
 {
     string SignUp(string userName,string password);////we just define the structure of the method SignUp

     string Login(string userName,string password);////we just define the structure of the method Login
 }
Enter fullscreen mode Exit fullscreen mode

and then I can go on to create another class and implement the interface I just created.

 public class Register : IRegister////this means inheriting the interface you created earlier
 {
   public string SignUp (string userName, string password)
///implementing the method SignUp from the interface IRegister 
   {
    ////Actual Logic to SignUp users

    return "User Registered Successfully";
 }

 public string Login(string userName, string password)
///implementing the method Login from the interface IRegister
 {
    ////Actual Logic to Login users....the actual logic to Login users is done here 

    return "Logged in Successfully";
  }
}
Enter fullscreen mode Exit fullscreen mode

What I have just done is provided an interface to my SignUp and Login method. Anyone who collects the assembly of my application cannot see the logic of my SignUp or Login method they only see the structure from my Register interface. This can be very handy if you want to hide the logic of your methods.

A class that inherits an interface must implement all its methods

Abstract Classes and Abstract Methods.

Abstract classes provide some sort of abstraction. The main difference between abstract classes and interfaces is that the methods in abstract classes can be partially implemented, fully implemented or not implemented at all. Unlike interfaces where no concrete implementation is done in the interface at all. In abstract classes, some methods can have their concrete implementation while some can only have the method structure.

Example:

Supposing in your Register class there was some logic to send Email and you wanted the concrete implementation of this method to be implemented and seen while also hiding the register and logic method implementation. This will be a good scenario to use abstraction.

Abstract classes are created with the abstract keyword
Methods that are abstract are created with the abstract keyword
Abstract methods can only be created in abstract classes

abstract class Register
{
    public sendEmail(string EmailAddress, string EmailBody)
    {
       ////logic to send email.....Concrete implementation done here
        return "Email Sent Successfully";
    }
    public abstract string SignUp(string user,string password);////abstract method....concrete implementation not done
    public abstract string Login(string user,string password);////abstract method....concrete implementation not done
}
Enter fullscreen mode Exit fullscreen mode

These Abstract methods can then be implemented in an other classes using the override keyword as shown below

public class Register2: Register////inheriting from the abstract class
{
    public override string SignUp(string user,string password)////the override keyword means you have overridden the abstract method in (Register) abstract class  and  you want to do your own logic
   {
   ////add your own logic to perform the SignUp method
     return "User Registered";
   }
}
Enter fullscreen mode Exit fullscreen mode

Differences

Interfaces Abstract Classes
No method has its concrete implementation Some methods can be implemented.some can just be defined.
Used when the concrete implementation of all the methods in a class are hidden Used when the implementation of (some) of the methods in a class are to be hidden
An abstract method is overriden Interfaces are implemented

Abstract vs interfaces

Abstract vs Interfaces in C#-Image from Quora

Abstraction helps to provide:

  1. One definition of a method and several concrete implementations.
  2. Ensures code security by helping to hide the logic of the methods you create.

Top comments (0)