What is cohesion and coupling in OOP?

Answer

Cohesion and coupling are fundamental design quality metrics that measure how well-organized your code is: Cohesion measures how strongly related and focused the responsibilities of a class are. High cohesion = a class has one, well-defined responsibility. Low cohesion = a class does too many unrelated things. High cohesion (good): class EmailValidator { public boolean isValidFormat(String email) { /* regex */ } public boolean domainExists(String email) { /* DNS lookup */ } public boolean isNotDisposable(String email) { /* check list */ } } // All methods relate to email validation -- single purpose. Low cohesion (bad): class Utility { public boolean validateEmail(String email) {} public int calculateTax(double price) {} public void sendNotification() {} public File parseCSV(String path) {} // Unrelated responsibilities! }. Coupling measures the degree of dependency between classes. Low coupling = classes are independent. High coupling = changes in one class cascade to others. Tight coupling (bad): class UserService { private MySQLDatabase db = new MySQLDatabase(); // Hardcoded dependency -- can't switch to PostgreSQL or mock! }. Loose coupling (good): class UserService { private Database db; // Depends on abstraction (interface) public UserService(Database db) { this.db = db; } // Can inject any Database implementation }. Goal: high cohesion + low coupling = modular, maintainable, testable code. Each class has one reason to change and is independent of others' implementation details.