I have difficulties with these two questions from OCP Java SE 6 Programmer Practice Exams from Bert Bates and Kathy Sierra :
Choices (not all shown): B. Class is well encapsulated
C. The setCity is an example of loose coupling
I said B and C. The study guide says B only. I don't get it. Doesn't having a good encapsulation (private variable and using a setter to modify the state of the object) promote loose coupling??. What is wrong here?
Choices (not all shown): A. These classes appear to have low cohesion
E. These classes appear to have weak encapsulation
F. These classes appear to have strong encapsulation
I said A and F. The guide says A and E. Given that there is a get and a set method, as well as other methods to modify the state of these objects, I was thinking as these classes being encapsulated. Anyway, they certainly appear to, given the pseudo-code. Again, what is wrong here?
Coupling is the relationship between two objects.
It is true that encapsulation promotes loose coupling. But in the Contact class example, there is no relationship with other objects.
Joined: Jul 29, 2012
For encapsulation, in this case, it is possible that Payroll class can directly access Employee's info (state). This is a weak encapsulation.
To make it strong encapsulated, Employee should have setInfo and getInfo methods. Payroll should access Employee's info via getInfo method instead accessing Employee's info directly.
In Employee class, it is possible that Employee can directly access the DB. It is possible that some DB configurations (DB URL, DB port number, DB username, password) are exposed to Employee!
Instead, Employee should create a DAO object, which encapsulates DB configurations. Then, Employee access the DB via the DAO object.
Similarly,Utilities class accesses the printer directly and printer configurations are exposed to Utilities. Instead,
Utilities class can access to the printer via a printer manager object, for example. This printer manager object encapsulates the printer's configuration.