People says "Encapsulation" is a process of wrapping code and data member into single unit is called Encapsulation", ok i agree with this, this is exact definition of it, but whats about understanding? is it sufficient to read, as we know definition is only for mug up, but for better understanding we should understand real time example and how really process is going on? ok i am explaining here, :)
As we know change is the law of universe, and in Java if you have to change anything then
"Whatever changes Encapuslates it" :)
we all have smart phone , suppose you are using whatsapp and now you want to change your profile image here you know the how you can change the profile image of whatsapp but dont know internal implementation, means you now how objects work only internal implementation are hidden from you, this is Encapsulation.
Encapsulation is implemented using private, public,default and protected access modifier.
The beauty of encapsulation is the power of changing things without affecting its users
Why Encapsulation use in Java Code?
Encapsulation is only for developer use.
Encapsulated Code is more flexible and easy to change with new requirements.
By using Encapsulation we can write immutable class in Java which are a good option in multi- threading environments.
Encapsulation uses abstraction.
Encapsulation allows us to change one part of code without affecting other parts of code.
As we know that for good software there must be loose coupling and high cohesion,s o Encapsulation is best example of this.
Remember one thing Java Bean is best example of Encapsulation,
>I know now you are thinking what is coupling
Coupling is the degree to which one class knows about another class.
Use of Encapsulation in Design Pattern
Factory pattern , Singleton pattern in Java makes good use of Encapsulation.--------------------->I will explain more with code :) <------------------
Thanks
Keep Learning.....
very helpful article
ReplyDeleteThanks this is what I was looking for.
Thanks Nadeem :)
ReplyDelete