I am reading a book preparing for the SCJP exam and it mentions that a programmer should strive for loose coupling but in practice classes will have some degree of coupling to actually be a functioning program. This makes sense. However, the chapter is cryptically concluded by the statement that "Of course, some classes, such as String and Math, are stand-alone classes and have no coupling to other classes." This makes no sense to me. If I use these classes in my class, aren't they coupled to that class? Does anyone have any thoughts on this?
These classes are loosely coupled meaning you are sending messages between your class and these classes like String so they work together. There is nothing like zero coupling.... At least classes are coupled using messaging.
I would see stand alone this way- String and Math classes operation doesnt depend on the state of other objects, they act on the data they have and produce the result.
maybe a class like this one? it doesn't depend on any other class once it is created, and i certainly found it useful.
i probably missed the point but oh well.
Coupling basically comes in to picture if two classes are related to each other. That is if they have a relationship by either "inheritence" or "composition". If a class extends another class, any changes made to the super class will impact the behaviour of the subclass. This means that the classes are tightly coupled. Interfaces should be preferred over the abstract classes for the same reason, i.e interfaces provides loose coupling, where as the classes are tightly coupled if inherited.
String class is an immutable and a final class, it cannot be inherited, hence there is no coupling.
Post by:autobot
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
a bit of art, as a gift, that will fit in a stocking