Skip to content

LSP Violation - SuperPrinter #4

@jossanchcald

Description

@jossanchcald

The SuperPrinter Class in the ex02 package violates the Liskov Substitution Principle, it overrides the print mehtod making it to be able to throw an exception, even if in its super class printer is able to do so, it does not throw any exception, so it violates the Liskov Substitution Principle

You should change your Printer Class from:
`package org.afdemp.cb6.ex02;

public class Printer {

public void print(String message) throws PrinterNotEnabledException {
    System.out.println(message);
}

}`

To the LSP Violation-free version:
`public class Printer {

private boolean enabled = true;

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

public void print(String message) throws PrinterNotEnabledException {
    if (!enabled) {
        throw new PrinterNotEnabledException("The printer is not enabled");
    }
    System.out.println(message);
}

}
`

This way SuperPrinter is able to work correctly and you don't violate the Liskov Substitution Principle.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions