Abstraction and Encapsulation



In an object-oriented programming language Encapsulation and Abstraction are very confusing words, everyone feels confused to understand.
Here I give in my own style of explanation.
Abstraction:
 The essential characteristics of an entity that distinguishes it from all other kinds of entities.

In other word,

“Only the characteristics of the system that are essential to the problem being studied are modelled; minor or irrelevant details are ignored.”

 It defines a boundary relative to the perspective of the viewer. It allows us to manage complexity by concentrating on the essential characteristics of an entity.

As for example

  •         A student is a person enrolled in classes in the university
  •          A professor is a person teaching classes at the university

Both are person but one is student and another is teacher which distinguishes both (essential to perspective of the viewer).

OO allows us to model our system using abstraction from the problem domain (for example, classes and objects).

Abstraction is done in Java.
  • When we conceptualize a class
  • When we write an ‘interface’
  • When we write an ‘abstract’ class
Encapsulation:

            Simply you can say that “information hiding”.

Hide implementation from client. Client depends on interface.

Thus, it’s possible to change the implementation without updating the clients as long as the interface is unchanged. It protects an objects internal state from being corrupted by its client.

As for example

Data inside the object is only accessible by the objects operations. No other object can reach inside the object and change its attribute value.

It should not be misunderstood that encapsulation is all about data hiding only. When we say encapsulation, emphasis should be on grouping or packaging or bundling related data and behaviour together.

Encapsulation is down in java:

·         When we make variable private or protected.
·         When we make method private or protected.









1 comment: