Encapsulation, Information Hiding And Abstraction


Encapsulation means drawing a boundary around something. It means being able to talk about the inside and the outside of it. Object-oriented languages provide a way to do this. Some provide several ways.

If you have been sick, you would have had medicines for sure. One of the tablets that you would have received is a capsule. I hope you would have seen it.

Now this kind of tablet, is a bit different from the others. From the outside its just a cap, and it hides everything that is contained within. But what lies inside may be 2 or 3 or more powders loosely arranged and packed within.

An object is something similar. It is created with the immense power of a class. while the composition of the class could be anything (as compared to the capsule), one may not know wht is contained when you create the object handle

as in
A obj = new A();

you may say obj here is like the capsule to all those who want to consume it in their programs.

So, with this object one can use its inherent power.

Thus this concept of hiding away its true power is known as Encapsulation

InformationHiding is the idea that a design decision should be hidden from the rest of the system to prevent unintended coupling. Encapsulation is a programming language feature. InformationHiding is a design principle.  InformationHiding should inform the way you encapsulate things, but of course it doesn't have to. They aren't the same thing.
Information hiding is technique for aiding developers in finding good modules, encapsulation is a technique for expressing the boundaries of those modules (and not the only one).

Encapsulation seems to be a combination of one or more of:
  • Grouping of relating things together
  • GateKeeper (state or data protection)
Information Hiding, on the other hand, is Hiding details of implementation

example of encapsulation without information hiding and/or information hiding without encapsulation

class NoEncapsulationOrInformationHiding { 
 public ArrayList widths = new ArrayList();
 }

 class EncapsulationWithoutInformationHiding {
 private ArrayList widths = new ArrayList();
 public ArrayList getWidths(){
  return widths;
 }
 }

 class InformationHidingWithoutEncapsulation {
 public List widths = new ArrayList();
 }

 class EncapsulationAndInformationHiding{
 private ArrayList widths = new ArrayList();
 public List getWidths(){
  return widths;
 }
 }

Encapsulation allows you to check access to your own innards, and provide specific methods of performing such access. It does not specifically address leaking implementation details; although you can control access to that variable, you can no longer control the fact that the client knows your using an ArrayList, even if you later decide to use LinkedList. Information Hiding, on the other hand is hiding the fact that your using an ArrayList to implement your widths. Although the client could still find out how your implementing it , you're no longer responsible for supporting that implementation.


Abstraction is to represent essential features of a system without getting involved in the complexity of the entire system.

The process of reducing an object to its essence so that only the necessary process of reducing an abject to its essence so that only the necessary element are represented abstraction defines an object in terms of its properties(attributes),behaviour(functionality), and interface.

Abstraction example: ATM is a good example of abstraction, we doesn't know what are all the internal processes that are going when we access it..instead we know only the inquiries, deposit, withdrawl etc...


Encapsulation-Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

In other words, encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions).
Ex:
public class Box
{
private int length;
private int width;
private int height;

public void setLength(int p)
{length = p;}

public void setWidth(int p)
{width = p;}

public void setHeight(int p)
{height = p;}

public int displayVolume()
{System.out.println(length*width*height)...
}

public static void main(String args [ ])
{
Box b1=new Box(3,4,5);
b1.displayvolume();
}
Abstraction-The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface).

Modifiers:
If you want to access a particular variable,method or class from anywhere,then make that variable accessible by using public modifier.

If you want to restrict a particular variable,method or class to be accessible outside the class,use private modifier.

Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers