Friday, 18 April 2014

Abstraction Vs Encapsulation

What is an Abstraction?
• Abstraction is thinking about something a certain way 
• Abstraction is the representation of only the essential features of an object and hiding un essential features of an object. 
• Through Abstraction all relevant data can be hide in order to reduce complexity and increase efficiency 
• Abstraction is simplifying complex reality by modeling classes appropriate to the problem 
• Abstraction-outer layout, used in terms of design 
• Encapsulation protects abstraction. 
• It taking required data and hiding the unwanted data.
In this above example abstraction shows only necessary details of car or shows only necessary details to drive a car like rear view mirror, gear, clutch, steering And hides internal detail of car like Piston, crankshaft, carburetors, gas turbines etc which is encapsulation for a car.

Abstraction shows only required data and hides unwanted data.

Difference between Encapsulation and Abstraction

1. Abstraction solves the problem in the design level1. Encapsulation solves the problem in the implementation level
2. Abstraction is used for hiding the unwanted data and giving relevant data2. Encapsulation means hiding the code and data in to a single unit to protect the data from outside world
3. Abstraction is a technique that helps to identify which specific information should be visible and which information should be hidden.3. Encapsulation is the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

In this above example abstraction shows only necessary details of car or shows only necessary details to drive a car like rear view mirror, gear, clutch, steering And hides internal detail of car like Piston, crankshaft, carburetors, gas turbines etc which is encapsulation for a car
Abstraction shows only required data and hides unwanted data .
In above example which shows encapsulated detail of a car which is not necessary to expose to outside world and make visible what is intended to be visible.

Difference between Encapsulation and Abstraction in OOPS


Difference between Encapsulation and Abstraction in OOPS

Abstraction and Encapsulation are two important Object Oriented Programming (OOPS) concepts. Encapsulation and Abstraction both are interrelated terms. 

Real Life Difference Between Encapsulation and Abstraction

Encapsulate means to hide. Encapsulation is also called data hiding.You can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. 

Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.

Implementation Difference Between Encapsulation and Abstraction

1.  Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.

2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.

3. Example of Encapsulation

Class Encapsulation
{
    private int marks;

    public int Marks 
   {
      get { return marks; }
      set { marks = value;}
    }
}

4. Example of Abstraction

abstract class Abstraction
{
    public abstract void doAbstraction();
}

public class AbstractionImpl: Abstraction
{
    public void doAbstraction()
   {
       //Implement it
   }
}

No comments:

Post a Comment