Concepts Of OOPS- Encapsulation And Abstraction

Whenever I prepare for interview, I always think of this basic concept and always find it confusing. Well, may be because I always thought about both of the hiding implementations. Also, I was under impression that encapsulation complements abstraction. I always had a question, if both hides implementation and complements each other then why two different names or why Encapsulation Vs Abstraction.

In the section, given below, I am going to discuss both. As per my understanding, encapsulation refers to- 

Encapsulation

In OOPS, an encapsulation allows us to wrap and control access of the logically grouped data. Yes, encapsulation means hiding the data. Lets see the class, given below. Its a simple class of Student, which encapsulates student properties and methods.
 
Code
  1. public class Student    
  2. {    
  3.    public string FirstName {getset;}    
  4.    public string LastName {getset;}    
  5.    public DateTime DateOfBirth {getset;}    
  6.    public bool Add(Student student)    
  7.    {    
  8.       ........Some Code    
  9.    }    
  10.    public List All()    
  11.    {    
  12.       ........Some Code    
  13.    }    
  14.    public bool Delete(Student student)    
  15.    {    
  16.       ........Some Code    
  17.    }    
  18. }    
Suppose, we want to implement one more class for teachers. I used to do is to copy student class and replace all Student with Teacher, because I always know the students and teachers both are human and have similar properties and methods.
 
Code
  1. public class Teacher    
  2. {    
  3.    public string FirstName {getset;}    
  4.    public string LastName {getset;}    
  5.    public DateTime DateOfBirth {getset;}    
  6.    public bool IsContract {getset;}    
  7.    public bool Add(Teacher student)    
  8.    {    
  9.       ........Some Code    
  10.    }    
  11.    public List All()    
  12.    {    
  13.       ........Some Code    
  14.    }    
  15.    public bool Delete(Teacher student)    
  16.    {    
  17.       ........Some Code    
  18.    }    
  19. }    
Abstraction

We canr also say, why not creating an interface or an abstract class and implement it to the class, whenever we want similar implementation. Yes, we can absolutely do that and it is the preferred way of doing it. What does Abstraction mean? Does it mean Abstraction complements Encapsulation? Not really. Here, we are only generalizing the behavior of similar objects. Also, abstraction always comes first, which means you draft your Application design on paper or UML diagram. Figure out similar objects and implement abstract classes or interfaces. In abstraction, we are not basically hiding the data but here, the goal is to overview behavior of the objects.

Code
  1. interface IMember   
  2. {  
  3.  bool Add(Member member);  
  4.  bool Delete(Member member);  
  5.  List Members();  
  6. }  
  7.   
  8. public class Student: IMember   
  9. {  
  10.   
  11. }  
  12. public class Teacher: IMember  
  13.  {  
  14.   
  15. }  
Conclusion: Encapsulation always means the data is hiding and controlling access, while abstraction means generalizing the objects. Hope, I explained it well. Please do not hesitate to leave the comments/corrections.
Enjoy!!!
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now