Harikrishna Gorrepati wrote:Loose coupling is part of encapsulation right ?
No Coupling is not a part of encapsulation, coupling use encapsulation, where the well-encapsulation can be achieved without loose coupling.
For example, I've a DSLR Camera(MyCamera) and two lenses of Different types which i switch between quite often as per my photography shoots requirement.
So lets say I have a Camera MyCamera & having 1 lens to take wide angle pictures and 2nd to take really close photos of any object.
check the following code:
The above code demonstrate well-encapsulation + loose coupling with each other. I can switch lenses easily from Wide Angle Lens to Macro Lens.
If I were use following code:
Now The MyCamera is coupled with WideAngleLens, so if i want to switch the lens of my camera to Macro lens for taking some really close pictures of objects then i have to Edit MyCamera class and change the lens, this is called tight coupling. However, you can see this code is good encapsulated but tightly coupled(not loosely coupled).
I tends to agree with pete stein. encapsulation gives great flexiblity to a programmar to change his logic[typically logic inside private method]. since private methods are not allowed to access directly by client.
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java