Interview Q&A / Object-Oriented Programming

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How do you spot a Single Responsibility violation in real code?

SHORT ANSWER

Look for a class whose description needs the word and, imports from unrelated areas, unrelated groups of fields, or edits from different people for different reasons.

Explain it simply

Imagine describing a class to a friend. If you say "it validates the form and sends the email and writes to the log", you have found three jobs. Other clues: the class imports both a database library and a PDF library; some methods use half the fields and other methods use the other half; merge conflicts keep happening in one file between teammates working on unrelated features.

Example

java
class OrderService {
    private Connection db;          // used by save()
    private SmtpClient smtp;        // used by notify()
    private TaxTable taxTable;      // used by price()

    Order price(Cart c)       { ... }   // uses taxTable only
    void  save(Order o)       { ... }   // uses db only
    void  notify(Order o)     { ... }   // uses smtp only
}

The fields split into three groups, and each method touches just one group. That is a strong sign of three classes hiding inside one.

A useful quick test: can you write the class's job in one sentence without "and"?

What to say in the interview

"I look at the reasons it would change. Signs are an and in its description, unrelated dependencies, methods that use separate groups of fields, and frequent merge conflicts. The fix is to extract each concern into its own class."

Common mistakes

  • Treating class size alone as proof. A large cohesive class can be fine.

#solid#srp#code-review

Related questions

Practise more Object-Oriented Programming questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Object-Oriented Programming quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.