Question

I'm currently learning about coupling and dependencies in Java. I've been reading this tutorial and understand that if class1 contains an instance of class2 and if you call a method like exampleMethod(c2), this counts as dependency between class1 and class2. However, I am unsure about the examples below. Can anyone give me some clarification?

Assuming that we are inside class1 (which contains class2 c2):

  1. If class1 calls a method defined in class2 (c2.aMethod() for example), does this count as a dependency/coupling between class1 and class2?

  2. Does calling c2.someSetterMethod(argument) count as dependency/coupling between class1 and class2?

  3. Does calling c2.repaint() within class1 count as a dependency?

Was it helpful?

Solution

The fact that class1 contains a reference to an instance of class2 suffices to say that class1 depends on class2. In all of your 3 examples you need a reference to class2 in order to call its methods. This only increases the need for dependency in your class1 but not the dependency itself.

Please read http://depfind.sourceforge.net/Manual.html#Dependencies as mentioned in my answer to your previous question: https://stackoverflow.com/a/21689495/1659599.

OTHER TIPS

  1. Yes.
  2. Yes.
  3. Yes.

One way to reduce coupling is to use interfaces. This way, class 1 only knows about the interface, and is not coupled with class 2 specifically. And in fact, any class that implements the interface could be used instead of class 2 and class 1 would still be satisfied.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top