• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, I thought that encapsulation required private variables and setters and getters, but this answer (question 24 on the mock exam 1 on the OCA online material) doesn't seem to agree with that, screenshot attached
My answers were C and F but apparently even E is correct because the explanation says that "Although setters are allowed in encapsulated class, they're not required." ANd that's my point, I thought that setters, together with getters were actually required...although come to think of it now, you can always use a constructor to initialize the variables...uhm..

question_24.jpg
[Thumbnail for question_24.jpg]
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is  not only testing about encapsulation. It is also testing about Immutability. You may already know how to make class as immutable . Just remove setter methods and insert constructor for variables initialization.


 
Gautham Muralidharan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So option E is correct or not?
 
Charissa Thomas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E is correct because it is only talking about encapsulation in that answer.  A setter is not required in order for a class member to be considered encapsulated.  The accessibility of the class member is how encapsulation is determined.  Can the class member be accessed directly without the class that owns it knowing?  This is what public access allows.  As soon as you set it as private, it is encapsulated and any changes to that member have to go through the class that owns it.  The presence of a setter method denotes a member as immutable or not (unless there are other methods that manipulate that member).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic