Harikrishna Gorrepati wrote:
Which of the following are Benefits of encapsulation
1. Code reusabiliy
2. Code clarity
3. Faster execution
4. Less dependancy on other classes
Given answers are 1 and 2. Why not option 4 ?
As far as I know, option 4 is one of the benefits of low coupling, not encapsulation.
Option 2, code clarity doesn't matter know ?
I'm sorry, but I don't understand what you're asking here.
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).
Hope this helps