LSP in Vehicle System
This program applies LSP to vehicle system.
Problem Statement
Write a Java program where subclass Car can replace superclass Vehicle.
Source Code
| 1 | class Vehicle { |
| 2 | void move(){ System.out.println("Vehicle moves"); } |
| 3 | } |
| 4 | class Car extends Vehicle { |
| 5 | void move(){ System.out.println("Car drives"); } |
| 6 | } |
| 7 | public class LSPVehicle { |
| 8 | public static void main(String[] args) { |
| 9 | System.out.println("Eduinq LSP Vehicle"); |
| 10 | Vehicle v=new Car(); |
| 11 | v.move(); |
| 12 | } |
| 13 | } |
Program Output
Eduinq LSP Vehicle Car drives
Explanation
LSP ensures subclass can substitute superclass. - Car replaces Vehicle without breaking functionality. - This maintains polymorphism and reliability in systems. Conclusion: Shows LSP applied to vehicle system.