wasAsked.

What are the SOLID principles in Java, with examples?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Java, Software Engineering

Community answers

Posted by wasAsked editorial collection

SOLID is a set of five object-oriented design principles that make Java code easier to maintain, test and extend. S — Single Responsibility Principle A class should have only one reason to change. Violation: class InvoiceService { BigDecimal calculateTotal(Invoice i) { ... } void saveToDatabase(Invoice i) { ... } void sendEmail(Invoice i) { ... } } Better: split into InvoiceCalculator, InvoiceRepository and InvoiceEmailSender. Each changes for a different reason (pricing rules, storage, email provider). O — Open/Closed Principle Classes should be open for extension but closed for modification. interface DiscountPolicy { BigDecimal apply(BigDecimal amount); } class FestivalDiscount implements DiscountPolicy { public BigDecimal apply(BigDecimal a) { return a.multiply(new BigDecimal("0.90")); } } class NoDiscount implements DiscountPolicy { public BigDecimal apply(BigDecimal a) { return a; } } class CheckoutService { private final DiscountPolicy policy; CheckoutService(DiscountPolicy policy) { this.policy = policy; } BigDecimal total(BigDecimal amount) { return policy.apply(amount); } } New discounts are added as new classes; CheckoutService does not change. L — Liskov Substitution Principle Subtypes must be usable wherever their base type is expected without breaking behaviour. Violation: class Bird { void fly() { ... } } and class Penguin extends Bird whose fly() throws UnsupportedOperationException. Code that calls fly() on any Bird breaks. Better: separate capabilities: interface Bird { void eat(); } interface FlyingBird extends Bird { void fly(); } class Sparrow implements FlyingBird { ... } class Penguin implements Bird { ... } I — Interface Segregation Principle Clients should not be forced to depend on methods they do not use. Violation: interface Worker { void work(); void eat(); } A Robot implementing Worker must implement eat(). Better: interface Workable { void work(); } interface Eatable { void eat(); } class Human implements Workable, Eatable { ... } class Robot implements Workable { ... } D — Dependency Inversion Principle High-level modules should depend on abstractions, not on concrete implementations. interface PaymentGateway { PaymentResult charge(Order order); } @Service class OrderService { private final PaymentGateway gateway; // abstraction OrderService(PaymentGateway gateway) { this.gateway = gateway; } // constructor injection void placeOrder(Order o) { gateway.charge(o); } } @Component class RazorpayGateway implements PaymentGateway { ... } Spring's dependency injection supplies the implementation, so OrderService can be tested with a mock gateway and switched to another provider without changes. How SOLID shows up in Spring Boot projects - Thin controllers, focused services and repositories (SRP). - Strategy beans selected by configuration (OCP). - Interfaces for repositories and gateways, implementations injected by Spring (DIP). - Small, focused interfaces such as Spring Data repository interfaces (ISP). Balance: apply SOLID where change is likely; over-applying it creates unnecessary interfaces and tiny classes. In interviews, give one real example where applying a principle simplified a change.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics