Thursday 14 January 2016

OOPs in Java- Encapsulation

OOPs in Java- Encapsulation   


One of the most fundamental concept of OOPs is Abstraction. Abstraction is a powerful methodology to manage complex systems. Abstraction is managed by well-defined objects and their hierarchical classification. For example a car in itself is a well-defined object, which is composed of several other smaller objects like a gearing system, steering mechanism, engine, which are again have their own subsystems. But for humans car is a one single object, which can be managed by the help of its subsystems, even if their inner details are unknown.
Java is an object oriented language because it provides the features to implement an object oriented model. These features includes encapsulation, inheritance and polymorphism.
OOPS is about developing an application around its data, i.e. objects which provides the access to their properties and the possible operations in their own way.

Principles of OOPs

Encapsulation

Below is a real-life example of encapsulation. For the example program and more details on this concept refer encapsuation in java with example.
Encapsulation is:
  • Binding the data with the code that manipulates it.
  • It keeps the data and the code safe from external interference
Looking at the example of a power steering mechanism of a car. Power steering of a car is a complex system, which internally have lots of components tightly coupled together, they work synchronously to turn the car in the desired direction. It even controls the power delivered by the engine to the steering wheel. But to the external world there is only one interface is available and rest of the complexity is hidden. Moreover, the steering unit in itself is complete and independent. It does not affect the functioning of any other mechanism.
Similarly, same concept of encapsulation can be applied to code. Encapsulated code should have following characteristics:
  • Everyone knows how to access it.
  • Can be easily used regardless of implementation details.
  • There shouldn’t any side effects of the code, to the rest of the application.
The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other.
A live example of encapsulation is the class of java.util.Hashtable. User only knows that he can store data in the form of key/value pair in a Hashtable and that he can retrieve that data in the various ways. But the actual implementation like, how and where this data is actually stored, is hidden from the user. User can simply use Hashtable wherever he wants to store Key/Value pairs without bothering about its implementation.
http://www.spcjaipur.com/java-training-institute-in-jaipur.aspx
 

0 comments:

Post a Comment