The Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclass without altering the correctness of the program. This principle is crucial for ensuring that derived classes extend the behavior of the base class without introducing unexpected issues.
Key Idea
A subclass should:
- Behave in a way consistent with its superclass.
- Not override or weaken the expectations of the base class.
If a subclass cannot fully substitute for its base class, it violates LSP.
Real-Life Example
Imagine a Restaurant program that calculates areas of shapes.
Violation of LSP
javaclass Bird { void fly() { System.out.println("I am flying!"); } } class Sparrow extends Bird { // Sparrow can fly, so no issue } class Penguin extends Bird { // Penguins cannot fly, but they are forced to implement fly() @Override void fly() { throw new UnsupportedOperationException("Penguins can't fly!"); } } public class Main { public static void main(String[] args) { Bird sparrow = new Sparrow(); sparrow.fly(); // Works fine Bird penguin = new Penguin(); penguin.fly(); // Throws exception! Violates LSP } }
Fixing the Violation (Applying LSP)
java// Base interface for all birds interface Bird { void eat(); } // Separate interface for birds that can fly interface FlyingBird extends Bird { void fly(); } // Sparrow can fly, so it implements FlyingBird class Sparrow implements FlyingBird { public void eat() { System.out.println("Sparrow is eating."); } public void fly() { System.out.println("Sparrow is flying."); } } // Penguin does not implement FlyingBird, only Bird class Penguin implements Bird { public void eat() { System.out.println("Penguin is eating."); } } public class Main { public static void main(String[] args) { FlyingBird sparrow = new Sparrow(); sparrow.fly(); // Works fine Bird penguin = new Penguin(); // penguin.fly(); // This line doesn't even compile, preventing errors } }
Now, only birds that can fly implement the fly()
method.Penguin
does not inherit fly()
, so no exceptions or unexpected behavior.
The code follows proper abstraction and inheritance, keeping it flexible and correct.
Comments (27)
SuperPH
Great breakdown on the mental game in Texas Hold’em-tournament players know how much it’s about reads and resilience. It’s similar to how platforms like SuperPH11 keep players engaged with live dealers and fast action.
JiliOK
Looking for a fresh twist on online gaming? JiliOK Login offers smart AI-driven insights that help you play smarter, not just harder. A real upgrade for casual and serious players alike.
SuperPH26
Just tried SuperPH and loved the 1024 paylines and wild Jacker Card feature-it adds real strategy to slot fun! Definitely a must-try for casual players.
Super Ace Jili
I love how slot games like Super Ace Jili mix strategy and luck. The wilds and free spins really boost the fun, especially for newcomers learning the ropes.
Super Ace Jili
Super Ace by Jili Games offers a thrilling mix of luck and strategy, especially with its 1024 ways to win and free spins feature. It’s no wonder it’s a hit in the Philippines-check out Super Ace Jili for more insights!
jljlph
Baccarat patterns can hint at trends, but discipline is key. Platforms like JLJLPH offer immersive live dealer experiences that blend strategy with entertainment-great for advanced play.
ph login
Solid article! Mobile gaming is where it’s at, and user experience is key. Seeing platforms like PH Login Casino really focus on that seamless mobile-first approach-like optimized interfaces-is impressive. Great insights here!
ph987 casino
It’s fascinating how platform design impacts player experience – a truly customer-first approach is key! Seeing that commitment to service at PH987 Login is impressive; optimizing those touchpoints really matters. It’s more than just games, right? 🤔
jljlph
Dice games offer a perfect blend of luck and strategy, and platforms like JLPH elevate this experience with their dynamic slot and live dealer options. It’s thrilling to see how innovation keeps the thrill fresh.
ph login
Really interesting points about mobile optimization! It’s key for engagement, and platforms like PH Login Casino seem to get that-intuitive interfaces are a game changer. User behavior analysis is so crucial these days!
Ghibli AI
Creating Ghibli-style photos feels like a dream come true, especially with tools like 지브리 AI. It’s amazing how AI can capture that magical essence without needing advanced art skills. Definitely a must-try for any creative!
ph222 login
That’s a really insightful point about player experience! Seamless access is huge, and platforms like PH222 seem to prioritize that-minimizing friction is key for engagement, right? Great article!
abc8 casino login
It’s fascinating how online gaming platforms are evolving – more than just luck now, it’s about skill! Seeing sites like ABC8 prioritize systematic development is a smart move. Check out their abc8 login for a streamlined experience & focus on strategic play – a real shift in approach!
ph987 login
Interesting take on maximizing returns! Thinking about long-term strategy is key. Platforms like PH987 are pushing boundaries with adaptive interfaces – anticipating player needs is smart. Good article!
ph987 login
Blackjack strategy is fascinating – mastering it feels like unlocking a new level! Seeing platforms like PH987 jili focus on innovative gaming experiences is cool; anticipating player needs is the future. It’s about more than just luck, right?
AI Manus
Interesting take on player psychology! Seeing how platforms like sz777 log in focus on user journeys-reducing friction is key. It’s all about that initial connection & building loyalty, isn’t it? Great article!
jljlph
Great insights on betting strategies! It’s crucial to balance risk and reward, especially with platforms like JLJLPH offering diverse games from slots to live dealers. Their secure interface and wide game selection make it a top choice for serious players.
ph365 casino
That’s a great point about player experience! It’s cool to see platforms like PH 365 Casino focusing on personalization – anticipating what players want is key to the future of gaming, right? Really innovative stuff!
jili777ok
It’s fascinating how platforms like Jili777 app blend AI with gaming to boost player engagement. The psychology of rewards is a powerful tool in design.
jkboss
That’s a great point about player psychology – understanding the odds is key! Platforms like jlboss are streamlining access, making it easier to jump in & enjoy the thrill, but smart play always wins! Secure logins are a must too.
jl boss slot
Really enjoying this article! It’s true, getting started with slots can feel overwhelming. jkboss makes it super easy with quick sign-up & verification – a great way to dive into the fun! Exploring different games is key. ✨
jlboss
Baccarat strategy is fascinating – understanding patterns can help, but it’s never a guarantee! Seeing platforms like jkboss offer diverse games is cool, though – more options for enjoying the thrill! It’s all about responsible fun, right? 😉
99win club
Basic strategy really shifts your perspective! Seeing how even small decisions impact the game is fascinating. Thinking of trying some new games – heard good things about 99win casino and their variety! Definitely a fun learning curve.
jljl55 ph
Interesting read! Seeing more localized gaming options like this is great for players. The focus on a smooth, user-friendly experience-especially the app-sounds promising. Check out jljl55 slot for a uniquely Pinoy gaming experience! 👍
33wim
Interesting read! Seeing more tech integration in online gaming-like the fast verification at 33wim slot-is crucial for both user experience & security. It’s a smart evolution for Vietnamese players!
13wim
Really insightful article! It’s great to see platforms adapting to local preferences – like offering Vietnamese-friendly payment options. Exploring a new 13wim game sounds fun, especially with options like Dragon Tiger! Definitely a growing market.
jl boss 2025
Smart bankroll management is key, even with exciting new platforms! Exploring options like JL Boss 2025 sounds fun, but remember to play responsibly. Check out this jlboss link – a streamlined experience is tempting, but caution is always best!