⛔️ Noncompliant Example
public class UserService {
public void saveUser(User user) {
// Save user to database
System.out.println("User saved to database: " + user.getName());
}
public void sendWelcomeEmail(User user) {
// Send welcome email to user
System.out.println("Welcome email sent to: " + user.getEmail());
}
public void logUserActivity(User user) {
// Log user activity
System.out.println("Logging activity for user: " + user.getName());
}
}
class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
✅ Compliant Example
public class UserRepository {
public void saveUser(User user) {
// Save user to database
System.out.println("User saved to database: " + user.getName());
}
}
public class EmailService {
public void sendWelcomeEmail(User user) {
// Send welcome email to user
System.out.println("Welcome email sent to: " + user.getEmail());
}
}
public class UserActivityLogger {
public void logUserActivity(User user) {
// Log user activity
System.out.println("Logging activity for user: " + user.getName());
}
}
class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
public class UserService {
private UserRepository userRepository = new UserRepository();
private EmailService emailService = new EmailService();
private UserActivityLogger userActivityLogger = new UserActivityLogger();
public void registerUser(User user) {
userRepository.saveUser(user);
emailService.sendWelcomeEmail(user);
userActivityLogger.logUserActivity(user);
}
}
⛔️ Noncompliant Example
public class ReportGenerator {
public void generateReport(String type) {
if (type.equals("PDF")) {
System.out.println("Generating PDF report...");
} else if (type.equals("HTML")) {
System.out.println("Generating HTML report...");
}
// If we need to add another format, we have to modify this method.
}
}
✅ Compliant Example
public interface Report {
void generate();
}
public class PDFReport implements Report {
@Override
public void generate() {
System.out.println("Generating PDF report...");
}
}
public class HTMLReport implements Report {
@Override
public void generate() {
System.out.println("Generating HTML report...");
}
}
public class XMLReport implements Report {
@Override
public void generate() {
System.out.println("Generating XML report...");
}
}
public class Main {
public static void main(String[] args) {
Report pdfReport = new PDFReport();
pdfReport.generate(); // Generating PDF report...
Report htmlReport = new HTMLReport();
htmlReport.generate(); // Generating HTML report...
Report xmlReport = new XMLReport();
xmlReport.generate(); // Generating XML report...
}
}
⛔️ Noncompliant Example
// Parent class Bird
public class Bird {
public void fly() {
System.out.println("Bird is flying");
}
}
// Child class Penguin that violates LSP
public class Penguin extends Bird {
@Override
public void fly() {
// Penguins cannot fly
throw new UnsupportedOperationException("Penguins cannot fly");
}
}
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.fly(); // Bird is flying
Bird penguin = new Penguin();
penguin.fly(); // Throws UnsupportedOperationException
}
}
✅ Compliant Example
// Interface for birds that can fly
public interface Flyable {
void fly();
}
// Base class Bird
public class Bird {
public void eat() {
System.out.println("Bird is eating");
}
}
// Class for a bird that can fly
public class Sparrow extends Bird implements Flyable {
@Override
public void fly() {
System.out.println("Sparrow is flying");
}
}
// Class for a bird that cannot fly
public class Penguin extends Bird {
// Penguins do not implement Flyable
}
public class Main {
public static void main(String[] args) {
Bird sparrow = new Sparrow();
sparrow.eat(); // Bird is eating
((Flyable) sparrow).fly(); // Sparrow is flying
Bird penguin = new Penguin();
penguin.eat(); // Bird is eating
// ((Flyable) penguin).fly(); // Compilation error, Penguin is not Flyable
}
}