Saturday, May 7, 2011

Abstraction v/s Encapsulation

Java is an Object Oriented Programming language. Each object oriented programming language is standing on the following pillars.

A. Polymorphism
B. Abstraction
C. Inheritance
D. Encapsulation

Now everyone who deals with Java code, surely understands inheritance and they have  a fair idea about polymorphism. Everyone knows inheritance because Java has forced developers to use inheritance. Each class inherits Object class internally. That is the reason we all understand Inheritance.

Polymorphism, is little more complex and less used concept. There are possibilities that developer may code thousand lines without encountering necessity of polymorphism in his code.

Abstraction and encapsulation are least used principle in Java programming language. Usually a beginner could not understand the analogy behind abstraction whereas  a beginner uses encapsulation logic by default. So we never try to explore these concepts until one fine day an interviewer asks the difference between Abstraction and Encapsulation. Seriously we should take interviews to increase our knowledge about technology.

If we need to define these principle's in two words then we can say Abstraction is implementation hiding where as encapsulation is Data Hiding.

What is Abstraction?
Class's definition is not complete and undefined definition is completed by child classes.
Weird, is not it? When there is no scope of creating a class then why having such class?

Database drivers are best example to illustrate abstraction. JDBC has defined a set of abstract interfaces/ abstract classes. Database providers have to complete the implementation for the following interfaces and classes. Now to interact with one database, developer need to register respective driver without changing the code. Developer does not need to mug up multiple databases to code for each of them. So using abstraction, problem of tackling different databases has been handled brilliantly. So without disclosing the actual implementation, desired functionality has been achieved.

What is Encapsulation?
Data is not directly accessible to user. It is much safer and recommended to make all fields as private and provide field accessors ( getter and setter methods ). Private is used so that no one could destroy the data intentionally and unintentionally and accessors methods provide more flexibility and control over the field.

Let's try to understand this with Walmart stores located in different countries. Let's say Walmart is trying to sell one product across countries. So for each country it will be inappropriate to have different instance of same product. To save the price of product they need to convert the price while storing and displaying, so having getter and setter methods give the control on data.

Here is the little difference between the use of abstraction and encapsulation. Hope I am able to clarify a little about complex and important concept of core Java.

No comments:

Post a Comment