How do you improve the coupling between these classes ?
1.) Mark int x=5 private and provide accessor fields to it
2.) Mark the variable z private
3.) Pass only the value of x instead of Main to quacker().
4.) Provide getters and setters for the variable z
5.) Remove IS A relation ship of Main with Duck
The above code is from CertPal mock exams
To increase coupling one class shouldn't know
about other class anything other than API
so (1) is correct as it restricts access of field of Main by Duck
so here I wounder why (2),(4),5) isn't correct and why (3) is correct
and also if we remove Is a Relationship then
Main and Duck loosly coupled in a better way perhaps as they can't know anything
about each other..
Also tell me when any class with strong encapsulation has tight coupling with other classes
is it possible that 2 class have strong encapsulation and tight coupling??
The first thing I would suggest is correct the indentation. Exam code often has incorrect indentation and it catches a lot of people out.
I think that 1 is incorrect (or partially incorrect); it says make x private and provide accessor methods. You should definitely make x private, but there already is an accessor method.
Yes, z should be private, even though it has no effect on the program shown at present.
3 means to change the parameter to take an int because it only requires an int. Yes, that would improve the coupling.
4 would be a good idea, but like 2 would have no effect.
I can see no point in the inheritance; I think that is simply put in to confuse you.
Please, everybody else, comment on this thread. I think those are poor questions; does anybody else know this book/website?
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop