You're refactoring an existing application to improve object-oriented design. You find a class Vehicle with methods like fly() and sail(). How would you refactor this class using IS-A and Has-A relationships to better adhere to the single responsibility principle?

To improve object-oriented design and comply with the Single Responsibility Principle (SRP), the Vehicle class should be refactored by separating unrelated behaviors into specialized classes and using composition where appropriate. A vehicle should only contain behaviors that are relevant to its type. Instead of placing fly() and sail() methods in a generic Vehicle class, create distinct vehicle types and compose them with reusable components.

Key Points

Example

An Airplane IS-A Vehicle and can fly, while a Boat IS-A Vehicle and can sail. Both vehicles may HAS-A Engine object. This prevents a Boat from having a meaningless fly() method or an Airplane from having an unnecessary sail() method.

class Engine {

    public void start() {

        System.out.println(
                "Engine Started");
    }
}

abstract class Vehicle {

    protected Engine engine =
            new Engine();
}

class Airplane extends Vehicle {

    public void fly() {

        engine.start();

        System.out.println(
                "Airplane is flying");
    }
}

class Boat extends Vehicle {

    public void sail() {

        engine.start();

        System.out.println(
                "Boat is sailing");
    }
}

public class Main {

    public static void main(String[] args) {

        Airplane airplane =
                new Airplane();

        Boat boat =
                new Boat();

        airplane.fly();
        boat.sail();
    }
}

Interview Tip

A concise interview answer is: I would remove unrelated methods from the Vehicle class and create specialized subclasses such as Airplane and Boat using the IS-A relationship. Shared functionality such as Engine would be implemented using composition (HAS-A relationship). This follows the Single Responsibility Principle, improves maintainability, and creates a cleaner object-oriented design.

Last updated: 16 July 2026

Previous question: You are designing an API for creating complex configuration objects for an application. Which design pattern would you choose to facilitate ease of use and flexibility in object creation?

Next question: You are working on a high-performance financial trading application that frequently updates prices and sorts them. Which Java collections would you use and why?

More Core Java - Level IV (Advanced) interview questions

All Core Java - Level IV (Advanced) questions