oop
25. SOLID: Liskov Substitution Principle (LSP)
Analyze the code, write your predicted output, and then reveal the solution.
The Scenario
The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
Predict the output and identify if LSP is violated.
class Bird { void move() { System.out.println("Flying..."); } } class Ostrich extends Bird { @Override void move() { throw new UnsupportedOperationException("I can't fly!"); } } public class Main { public static void main(String[] args) { Bird b = new Ostrich(); try { b.move(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.