Tuesday, September 20, 2016

Interview Question Series - Abstraction and Encapsulation

Abstraction means in object oriented design that , a class should and only include the required functions or methods. like if there is a class of Customer, it should not include any order related details. and it should have only required properties or methods of customer which is going to use in implementation of that class.

Abstraction means to show only the necessary details to the client of the object

another similar keyword is "Abstract Class"
Abstract class in OOD is like interface but can not create objects , it has only required methods/functions which can be used in derived / implemented class.
like a vehicle class can not create object but another derived class of this like CAR can create object or Hyundai, Maruti etc.


Encapsulation is used to hide its members from outside class or interface, this can be done with access modifiers, like public ,private, internal, protected and protected internal. also with proper implementation of get and set properties.

for example, in employee class the calculation of age is not required outside the class, so that method can be created as private and properties of get and set can be defined for date of birth and only get can be defined for Age. there is object have only age get option, there is no set for age property and no calculation method both are hidden inside the class based on encapsulation concept.

details of access modifiers
  • Public: Access to all code in the program
  • Private: Access to only members of the same class
  • Protected: Access to members of same class and its derived classes
  • Internal: Access to current assembly
  • Protected Internal: Access to current assembly and types derived from containing class.







No comments:

Post a Comment