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.
posted 8 years ago
That's what I thought, too, but the book suggests otherwise. I'm not sure what a "stand-alone" class really is or how it would be useful.
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.