Get ready for your next interview with our comprehensive question library
SOLID is an acronym representing five fundamental design principles for object-oriented programming that promote clean, maintainable, and extensible code:
Benefits of following SOLID principles:
The Single Responsibility Principle states that a class should have only one reason to change, meaning it should have only one responsibility or job. A class should focus on a single aspect of the software's functionality.
Example of violation:
class User {
private String name;
private String email;
// User data management
public void setName(String name) { this.name = name; }
// Email functionality - violates SRP
public void sendEmail(String message) {
// email sending logic
}
// Database operations - violates SRP
public void saveToDatabase() {
// database saving logic
}
}
Better approach:
class User {
private String name;
private String email;
// Only user data management
}
class EmailService {
public void sendEmail(User user, String message) { }
}
class UserRepository {
public void save(User user) { }
}
The Open/Closed Principle states that software entities should be open for extension but closed for modification. You should be able to add new functionality without changing existing code.
Example:
// Violates OCP - need to modify for new shapes
class AreaCalculator {
public double calculateArea(Object shape) {
if (shape instanceof Rectangle) {
Rectangle r = (Rectangle) shape;
return r.width * r.height;
} else if (shape instanceof Circle) {
Circle c = (Circle) shape;
return Math.PI * c.radius * c.radius;
}
return 0;
}
}
// Follows OCP - extensible without modification
interface Shape {
double calculateArea();
}
class Rectangle implements Shape {
public double calculateArea() {
return width * height;
}
}
class AreaCalculator {
public double calculateArea(Shape shape) {
return shape.calculateArea();
}
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime