• Post Reply Bookmark Topic Watch Topic
  • New Topic

Benefits of encapsulation  RSS feed

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This question is from Whizlabs mock exam

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 ? Option 2, code clarity doesn't matter know ?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is, Why Option 2 is correct choice and why option 4 is not correct choice. Loose coupling is part of encapsulation right ?
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loose coupling is not necessarily an advantage of encapsulation. Even if you use encapsulation but all your classes heavily depend on each other, your code is not loosely coupled...
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Minhaj
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!