Skip to main content

Practice Problems for Interface Segregation Principle

Interface Segregation Principle (ISP)

Practice Problems

To master the Interface Segregation Principle (ISP), it's essential to engage in practical exercises and projects. Here are some complete coding exercises, sample projects, quizzes, and multiple-choice questions (MCQs) to help reinforce your understanding.

Code Refactoring Exercise

Given:

A code snippet that violates ISP by having a single interface overloaded with methods:

interface Worker {
void work();
void eat();
void sleep();
}

class Employee implements Worker {
@Override
public void work() {
System.out.println("Working");
}

@Override
public void eat() {
System.out.println("Eating");
}

@Override
public void sleep() {
System.out.println("Sleeping");
}
}

class Robot implements Worker {
@Override
public void work() {
System.out.println("Working");
}

@Override
public void eat() {
// Robots don't eat
throw new UnsupportedOperationException("Robots don't eat");
}

@Override
public void sleep() {
// Robots don't sleep
throw new UnsupportedOperationException("Robots don't sleep");
}
}

Challenge: Refactor the code to adhere to ISP.

Refactored:

interface Worker {
void work();
}

interface Eater {
void eat();
}

interface Sleeper {
void sleep();
}

class Employee implements Worker, Eater, Sleeper {
@Override
public void work() {
System.out.println("Working");
}

@Override
public void eat() {
System.out.println("Eating");
}

@Override
public void sleep() {
System.out.println("Sleeping");
}
}

class Robot implements Worker {
@Override
public void work() {
System.out.println("Working");
}
}

Design Challenge

Challenge: Design a class hierarchy for a home automation system that adheres to ISP. The system should have base interfaces for LightControl, ThermostatControl, and SecurityControl. Each interface should provide methods relevant to controlling lights, thermostats, and security systems, respectively.

Coding Exercises

  • Exercise 1: Implement a Device Control System:
    • Description: Create a base interface DeviceControl with methods turnOn and turnOff. Extend this interface for specific devices like LightControl and FanControl, adding device-specific methods. Implement these interfaces in classes representing different types of devices.
    • Example:
interface DeviceControl {
void turnOn();
void turnOff();
}

interface LightControl extends DeviceControl {
void dim();
}

interface FanControl extends DeviceControl {
void setSpeed(int speed);
}

class SmartLight implements LightControl {
@Override
public void turnOn() {
System.out.println("Light is on");
}

@Override
public void turnOff() {
System.out.println("Light is off");
}

@Override
public void dim() {
System.out.println("Dimming light");
}
}

class SmartFan implements FanControl {
@Override
public void turnOn() {
System.out.println("Fan is on");
}

@Override
public void turnOff() {
System.out.println("Fan is off");
}

@Override
public void setSpeed(int speed) {
System.out.println("Setting fan speed to " + speed);
}
}

Sample Projects

  • Project 1: Home Automation System:
    • Description: Design a home automation system with interfaces for controlling lights, thermostats, and security systems. Implement these interfaces in classes representing smart home devices. Ensure that each interface provides only the methods relevant to the specific device.
    • Example:
interface LightControl {
void turnOn();
void turnOff();
void dim();
}

interface ThermostatControl {
void setTemperature(int temperature);
}

interface SecurityControl {
void arm();
void disarm();
}

class SmartLight implements LightControl {
@Override
public void turnOn() {
System.out.println("Light is on");
}

@Override
public void turnOff() {
System.out.println("Light is off");
}

@Override
public void dim() {
System.out.println("Dimming light");
}
}

class SmartThermostat implements ThermostatControl {
@Override
public void setTemperature(int temperature) {
System.out.println("Setting temperature to " + temperature);
}
}

class SmartSecuritySystem implements SecurityControl {
@Override
public void arm() {
System.out.println("Arming security system");
}

@Override
public void disarm() {
System.out.println("Disarming security system");
}
}

Quizzes

  • Quiz 1: Identifying ISP Violations:
    • Question: Given the following interface, identify the ISP violation and suggest a way to fix it.
interface Appliance {
void turnOn();
void turnOff();
void setTemperature(int temperature);
}

class Heater implements Appliance {
@Override
public void turnOn() {
System.out.println("Heater is on");
}

@Override
public void turnOff() {
System.out.println("Heater is off");
}

@Override
public void setTemperature(int temperature) {
System.out.println("Setting temperature to " + temperature);
}
}

class Fan implements Appliance {
@Override
public void turnOn() {
System.out.println("Fan is on");
}

@Override
public void turnOff() {
System.out.println("Fan is off");
}

@Override
public void setTemperature(int temperature) {
// Fans don't set temperature
throw new UnsupportedOperationException("Fans don't set temperature");
}
}
  • Answer:
interface Appliance {
void turnOn();
void turnOff();
}

interface TemperatureControl {
void setTemperature(int temperature);
}

class Heater implements Appliance, TemperatureControl {
@Override
public void turnOn() {
System.out.println("Heater is on");
}

@Override
public void turnOff() {
System.out.println("Heater is off");
}

@Override
public void setTemperature(int temperature) {
System.out.println("Setting temperature to " + temperature);
}
}

class Fan implements Appliance {
@Override
public void turnOn() {
System.out.println("Fan is on");
}

@Override
public void turnOff() {
System.out.println("Fan is off");
}
}
  • Quiz 2: ISP and SRP:
    • Question: How does adhering to ISP support the Single Responsibility Principle (SRP)? Provide examples.
    • Answer:
      • Explanation: Adhering to ISP supports SRP by ensuring that interfaces are focused and specific, reducing the likelihood of classes being burdened with multiple responsibilities. For example, instead of a Vehicle interface with methods for driving and flying, separate interfaces like Driveable and Flyable ensure that each class has a single responsibility.

Multiple-Choice Questions (MCQs)

  • MCQ 1: What does the Interface Segregation Principle state?

    • A) Classes should inherit from a single base class
    • B) No client should be forced to depend on interfaces it does not use
    • C) Classes should be open for extension but closed for modification
    • D) Methods should only be implemented once

    Answer: B) No client should be forced to depend on interfaces it does not use

  • MCQ 2: Which of the following is a violation of ISP?

    • A) An interface with methods for different functionalities
    • B) A class that implements multiple small interfaces
    • C) An interface that provides methods for a single functionality
    • D) A class that depends on an interface with methods it doesn't use

    Answer: D) A class that depends on an interface with methods it doesn't use

  • MCQ 3: How can you implement ISP in a large system?

    • A) By creating a single interface with all methods
    • B) By dividing functionality into smaller, focused interfaces
    • C) By using inheritance to extend a large interface
    • D) By avoiding the use of interfaces

    Answer: B) By dividing functionality into smaller, focused interfaces

By working through these extensive practice problems, quizzes, and MCQs, you'll gain hands-on experience with the Interface Segregation Principle, helping you to apply it effectively in your projects. These exercises will deepen your understanding and improve your ability to create maintainable, scalable, and flexible code.