Thursday, October 23, 2014

Abstraction And Encapsulation


Encapsulation:
  • The internal representation of an object is generally hidden from view outside of the object's definition. 
  • Encapsulation, refers to an object's ability to hide data and behavior that are not necessary to its user.
  •  Classes, Properties and Access Modifiers are tools to provide encapsulation, in c#.
  • Its hiding information details from the caller.
  • Its hiding implementation.
  • Its hiding data like using getter and setter etc.
  • Encapsulation is not just private fields with public properties, but it is both the data and the process (or behavior) being hidden or wrapped up in an implementation that is not exposed to the outside world.
  • We also hidden, the process of converting the data into information. So we say that, we encapsulate the data and the process, crating an object that has both data and behavior.
  • A human being, is not just height,width,eye color,hair color and other data points. A human is also a set of behaviors (influenced by many things, including genetics,life experience etc ) which make each of us unique.
  • it's used to restrict access to the members of a class.
  • Any object that stores details of that object. Example Employee object may have Name, EmployeeId, Salary information etc. By using encapsulation, Employee object can expose the data (ex. Name,EmployeeId) and methods (ex. GetSalary()) necessary for using the object,  while hiding its irrelevant fields and methods from other objects. Here all users could access basic information about an employee while restricting salary information.
Exposing a solution to a problem without requiring the consumer to fully understand the problem domain.
Abstraction:
  •  Abstraction means to show only necessary details to the client of the object.
    • Specific solution for a problem, we can use abstraction and we can re-use this solution for the same domain or even for different domain.
    • So we can say that, for re-usability  we use abstraction.
     
  • Interfaces, Abstract classes or Inheritance and Polymorphism are tools to provide abstraction, in c#.
  • Abstraction means -  hiding implementation using abstract class or interface etc..
  • Abstract class provide abstraction but How? 
           Ans: In .net framework, we use list or collection. Imagine if .net only implemented a StudentList And TeacherList, that could only hold a list of students and list of teachers. But what if we need department list. So the "list" or "collection" abstracted away from student,teacher and department. The list or collection by itself is an abstract concept.  


Abstraction Vs Encapsulation:
  • Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired  level of abstraction.
  • Abstraction is a technique that helps up identify which specific information should be visible and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to visible. -Edward V. Berard
.Net design guideline is that, data should be exposed via properties. 
Client of the object or Outside of the object means when we use reference of object then it will show only necessary methods and properties. And hide methods which are not necessary.

No comments:

Post a Comment