Concepts Of Object-Oriented Programming -Part 3(Abstraction)
Tags: csharp, objectorientedprogramming, dotnet
Published: 2020-06-18 10:57:57


Welcome to the part 3 of our exploration into Object Oriented Programming. If you have not read part two check it out [encapsulation](https://dev.to/gbengelebs/principles-of-object-oriented-programming-part-1-encapsulation-10ek). Part 1-[Inheritance](https://dev.to/gbengelebs/principles-of-object-oriented-programming-part-1-inheritance-308e). 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](https://dev-to-uploads.s3.amazonaws.com/i/1b7mbg22q8ocbh6et8h1.png)
<figcaption>Abstraction vs Encapsulation</figcaption>

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](https://dev-to-uploads.s3.amazonaws.com/i/c3nku2qj3o6bp8urlbom.jpg)
<figcaption>Interfaces in C#</figcaption>

Lets create an interface called Register. By convention  interfaces start with the capital letter I
Example:
```csharp
 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
 }
```
and then I can go on to  create another class and implement the interface I just created.

```csharp
 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";
  }
}
```

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*

```csharp
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
}
```
These  Abstract methods can then be implemented in an other classes using the override keyword as shown below

```csharp
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";
   }
}
```

##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 ](https://dev-to-uploads.s3.amazonaws.com/i/6incup5rystvr58ycnz9.png)
<figcaption>Abstract vs Interfaces in C#-Image from Quora</figcaption>

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.

</p>