• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following test question was taken from enthuware.

Consider the following two classes (in the same package but defined in different source files):





You are assigned the task of refactoring the Square class to make it better in terms of encapsulation.
What changes will you make to this class?

Select 4 options
a. Add a calculateArea method:
private void calculateArea(){
this.area = this.side*this.side;
}
b. Make side and area fields private.
c. Change setSide method to:
public void setSide(double d){
this.side = d;
calculateArea();
}
d. Make the getArea method public.
e. Add a setArea() method:
public void setArea(double d){ area = d; }

Answer: a, b, c, d

Explanation:
e. This is not required because area is calculated using the side.
So if you allow other classes to set the area, it could make side and area inconsistent with each other.
__________________________________________________________________________________________________
I understand the key concept behind encapsulation is :
1) Make those member fields associated with getters and setters private
2) Make your getter and setter methods ( accessors and mutators) public


Question1: For option a) the reason calculateArea() is not public is because it is not a getter/setter (accessor/ mutator)?


__________________________________________________________________________________
Question2: Should I only make those methods public that are accessors/ mutators?

For option c), one has to realize that in order to be able to call calculateArea() from setSide(),
it first has to be created in option a).
In other words, option c) is dependent on option a).

The current given code does not contain the method calculateArea().
__________________________________________________________________________________
Question 3:
Is the way that area is currently being calculated (within TestClass) violate the principle of encapsulation?

That is, a programmer that makes use of the class "Square" does not need to know how calculateArea() is performed?

__________________________________________________________________________________
Question 4: Is the reason we make the access modifier for

calculate Area()

private is because it is not an 1) accessor or 2) mutator ?

__________________________________________________________________________________
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept behind encapsulation is that you make public (or exposed) only those things that MUST be made public, and you keep everything that the user doesn't need to know to yourself.

To step backward from your question for a moment, let's think about this in real-world. You have a square and need to do some math with it (building a fence or something). You know how long each side of the square is, and you know that the area of a square is dependent on that. If you change the length of one side, the area will change.

So if we implement the class with calculateArea() public, the user can force an area calculation to occur at any time. Depending on the design of the class, calculating the area() could be required to make it update. But we know what the area is given the side length - so why not hide that detail from the user. Additionally, if we decide in the future that we don't need a separate method to calculate the area, we can just do it every time getArea() is called, we can remove the calculateArea() method without hurting the user at all.

There are no real hard and fast rules for what is made public and what is not. For the exam, it's probably enough to assume that accessors and mutators should be public, internal processing private. Just be aware that there are real-world exceptions to the rule. (For example, if calculateArea() was a very expensive call and you wanted the client to control when it was called, that might be a good reason to make it public.)

To directly answer your questions:
1. calculateArea() is not public because other classes have no use for it.
2. Generally, yes, unless you have a good reason to make them public.
3. I wouldn't say it violates encapsulation - you do provide the getArea() method, they're just choosing not to use it. It would violate encapsulation if YOU exposed in the Square how the area was calculated to the user. What they do with your class is their business.
4. Yes, it is private because you do not want to expose this info.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic