• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Benefits of encapsulation

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic