Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Coupling covered in the OCAJP7 exam?

 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I was reviewing SCJP6 by K&B and got to the topic Coupling, is it part of the Certification exam?

if yer or no, can someone please explain coupling in a simple and straightforward answer with an example?
I am quite confused with it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coupling is not on the OCAJP7, but on the OCPJP7. So no need to worry.

It's actually not that hard. So I'll try to explain coupling and give an example as well. Let's go!

Coupling is the degree to which one class knows about another class. You should always aim for loosely coupled classes, meaning class A doesn't know anything about the implementation of B, it only knows what class B has exposed through its interface (as in public methods). Tightly coupled classes are bad: class A knows implementation details about class B. Why is tight coupling considered bad? That's shown in the following example.

Let's assume for the sake of this example you have written these classes:


And I have written this class:


Classes Pizza and PizzaOrderingSystem are tightly coupled, because PizzaOrderingSystem knows (and depends on) some implementation details of Pizza (it's using an array to store the ingredients). Tight coupling is BAD. Why? What would happen if the class Pizza was changed/enhanced and a list was used instead of an array to store the ingredients? Mayhem! Coincidence or not, but that's shown in the next code snippet.

You are preparing for the OCAJP7 and you learnt about the ArrayList and how it makes array manipulation a lot easier. So you decide to have some code practice ( and change/enhance your Pizza class to use an ArrayList instead (but you don't know the PizzaOrderingSystem class exists). The Pizza class now looks like:


I get this new version of the Pizza class and see my PizzaOrderingSystem class is broken and has some compiler errors now. So I fix them. The updated PizzaOrderingSystem class:


After passing the OCAJP7 certification, you decide to prepare for the OCPJP7. In the chapter about generics & collections you learn about the Set interface: it's used when you want no duplicates in your collection. So you decide that code practice is the best way to prepare you for the exam (and that's so true ) and decide a pizza can't have the same ingredient more than once. So you change the Pizza class once more:


I get this new version of the Pizza class and oh no... again compiler errors in my PizzaOrderingSystem class Instead of fixing this once more I decide to give you a skype call and give you an informative explanation about coupling and why it's a good thing to hide implementation details in your classes. And as a thank you for my effort you decide to add a getTotalPrice method too. The new and improved Pizza class:


With this new and improved class I can change/simplify my PizzaOrderingSystem class:


Both Pizza and PizzaOrderingSystem classes are now loosely coupled: they do not know (or depend on) each other implementation details! So now I can add another method to the PizzaOrderingSystem class to print an overview of the ingredients:


Because both classes are loosely coupled I don't have to worry that another implementation change in the Pizza class will affect my code again. Just to prove: you are making good progress in the generics & collections chapter and you are now studying about the Map class to store key-value pairs. Once again you decide to change the Pizza class:


Because the Pizza class didn't change its (public) API, but only some implementation details (how the ingredients are stored) I can use the Pizza class without having to change anything in my PizzaOrderingSystem class. And that's all thanks to the loose coupling between both classes. Hooray! No matter what implementation you use in the Pizza class to store the ingredients you have committed yourself to always return the ingredients in a List, no matter what you are using internally to store them (an array, a set, a map,...). So if you change the internal data structure, you'll probably need to make some changes in other methods too, to preserve your (public) API (as shown in the example above: using a Map required changes in methods getIngredients & getTotalPrice). Always be careful when making changes to the (public) API of a class, it might break other classes using your class (think about Java's backward compatibility). Even if your classes are loosely coupled, that's something that still could happen. But (public) API changes are less likely than implementation changes.

Whoa! That is without any doubt the longest post I have ever made here on the ranch.

Hope it helps!

(Disclaimer: for the OCAJP7 exam you only need to know ArrayList. Coupling, List, Set and Map are only on the OCPJP7; they were just used here for demo purposes only.)
 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastico!

I thought I'm gonna be reading this for a whole day.
Anyways, your explanation was clear and now I understand it. I
hope it'll stay on my head till my OCPJP7 comes.

Now I know that coupling has something to do with the implementation of classes.
Many Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Cardenas wrote:I thought I'm gonna be reading this for a whole day.

Good to hear you needed whole day to read it, I needed a whole day to write it

Ed Cardenas wrote:Anyways, your explanation was clear and now I understand it. I hope it'll stay on my head till my OCPJP7 comes.

Glad to hear the explanation was helpful. Probably because you played one of the leading parts in the story (more involvement/engagement ). You can always bookmark that thread and read it again if you are preparing for the OCPJP7.

Ed Cardenas wrote:Now I know that coupling has something to do with the implementation of classes.

Most of the times coupling is related to a class not being well encapsulated (as in the above examples). On the exam you'll only be tested on very obvious cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic