• 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

K&B encapsulation concept

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


given the following:


which is true about the classes described above?
A. Class A is tightly encapsulated.
B. Class B is tightly encapsulated.
C. Classes A and B are both tightly encapsulated.
D. Neither class A nor class B is tightly encapsulated.


The answer is obviously D, but my question is regarding the explanation and the concept in general:

At first glance class B appears to be encapsulated...


Well, the principle of encapsulation says that the class should have all of its state private while providing public getters and setters to access and modify the state. There is only a setter in B, so would we still consider it being tightly encapsulated if foo variable in A were declared private?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if the variable in class A had been declared private then the subclass B would be tightly encapsulated.
Encapsulation requires member variables to be declared private. There is no requirement to add getter and setter methods if they are not needed for your application.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan, this really helps clear the concept.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait... something's weird here...
Was that Dan making a post ?? Wow!
Hey Dan! How've ya been?

-Bert
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's me!
I've been fine. I wish I could offer a good excuse for my disappearance, but I can't. There was no alien abduction or anything interesting like that. I can't even claim a hostage situation. Last spring, I went on vacation and just didn't get back into ranching when I returned.
Judging by your posts, it appears that you have been very busy and doing well here at the Ranch.
I'll try not to make a stranger of myself in the future.
It's nice to be back.
 
reply
    Bookmark Topic Watch Topic
  • New Topic