Does Overriding Violate LSP? Understand the Risks and Rules
What Exactly Is LSP and Why Does It Matter?
LSP stands for Liskov Substitution Principle, one of the SOLID principles of object-oriented design. In short, it says: "Objects of a superclass should be replaceable with objects of its subclasses without breaking the application."
Sounds simple, right? But here’s the catch: just because your code compiles doesn’t mean it follows LSP.
Let’s break it down.
A real-world-ish analogy
Imagine a class Bird
with a method fly()
. Then you create a subclass Penguin
that also extends Bird
. Uh-oh. Penguins don’t fly. If someone writes code expecting every Bird
to fly, plugging in a Penguin
breaks that assumption. Boom — LSP violation.
Moral of the story: behavior matters more than type.
So… Does Overriding a Method Break LSP?
Short answer: Not necessarily.
Method overriding is actually how polymorphism works. It’s part of the core design of OOP. But — and this is big — how you override matters.
If you override a method and change the behavior so that the subclass no longer “fits” in the place of its parent, then yeah, you’ve violated LSP.
Let me give you an example that bit me once in a real project.
I had a base class Shape
with a method getArea()
. All good. Then I created a BrokenCircle
class (I know, dumb name) that returned -1
if the radius was zero. I thought I was being “safe”. But another function, which aggregated areas, totally misbehaved — it expected all Shapes
to return a non-negative area. Whoops. That override? Violated LSP hard.
What you can override safely
You can override when:
The method still returns compatible results
You don’t introduce unexpected side effects
You honor the contracts (preconditions, postconditions, invariants) of the base class
Basically: follow the rules of the parent. Don’t get fancy just because you can override.
Red Flags: How to Know You're Violating LSP
Behavioral changes
If the subclass behaves too differently from the parent class, you’re in trouble. Even if the signature matches, the intent doesn’t.
Example: A base class File
has open()
. A subclass ReadOnlyFile
throws an exception when you call open()
because it's not meant to be opened that way. Boom — not substitutable anymore.
Narrowing input expectations
If a base class method accepts all integers, but your subclass only works with positive integers, then… yeah. You've just shrunk the valid input space. That’s a subtle but classic LSP break.
Unexpected exceptions
If the base class never throws, but your override suddenly tosses custom exceptions, it forces the caller to handle stuff they didn’t sign up for.
Been there. Not fun.
How to Override Without Breaking LSP
Follow Design by Contract
Preconditions: A subclass must not require more than its superclass.
Postconditions: A subclass must guarantee at least what the superclass promises (or more).
Invariants: State consistency must be preserved.
It sounds formal, but honestly, it's just good sense. Don’t make subclasses weirder than the parent.
Use composition if behavior diverges
Sometimes overriding just doesn’t make sense. If you find yourself twisting logic to override something in an unnatural way, consider using composition over inheritance.
I once replaced a subclass with a helper class that was injected into the parent, and guess what? Cleaner code, no LSP issues, and way easier to test.
Conclusion: Overriding Is Fine — If You Respect the Contract
Overriding methods doesn’t inherently violate LSP — but it’s dangerously easy to do it wrong. Just because your class compiles and runs doesn’t mean it’s safe in the long run.
Think like this: “If I replace the parent with this subclass, will the program behave the same way?” If the answer is no… well, you’ve probably broken LSP. Time to refactor, my friend.
So override away — but tread carefully.
How much height should a boy have to look attractive?
Well, fellas, worry no more, because a new study has revealed 5ft 8in is the ideal height for a man. Dating app Badoo has revealed the most right-swiped heights based on their users aged 18 to 30.
Is 172 cm good for a man?
Yes it is. Average height of male in India is 166.3 cm (i.e. 5 ft 5.5 inches) while for female it is 152.6 cm (i.e. 5 ft) approximately. So, as far as your question is concerned, aforesaid height is above average in both cases.
Is 165 cm normal for a 15 year old?
The predicted height for a female, based on your parents heights, is 155 to 165cm. Most 15 year old girls are nearly done growing. I was too. It's a very normal height for a girl.
Is 160 cm too tall for a 12 year old?
How Tall Should a 12 Year Old Be? We can only speak to national average heights here in North America, whereby, a 12 year old girl would be between 137 cm to 162 cm tall (4-1/2 to 5-1/3 feet). A 12 year old boy should be between 137 cm to 160 cm tall (4-1/2 to 5-1/4 feet).
How tall is a average 15 year old?
Average Height to Weight for Teenage Boys - 13 to 20 Years
Male Teens: 13 - 20 Years) | ||
---|---|---|
14 Years | 112.0 lb. (50.8 kg) | 64.5" (163.8 cm) |
15 Years | 123.5 lb. (56.02 kg) | 67.0" (170.1 cm) |
16 Years | 134.0 lb. (60.78 kg) | 68.3" (173.4 cm) |
17 Years | 142.0 lb. (64.41 kg) | 69.0" (175.2 cm) |
How to get taller at 18?
Staying physically active is even more essential from childhood to grow and improve overall health. But taking it up even in adulthood can help you add a few inches to your height. Strength-building exercises, yoga, jumping rope, and biking all can help to increase your flexibility and grow a few inches taller.
Is 5.7 a good height for a 15 year old boy?
Generally speaking, the average height for 15 year olds girls is 62.9 inches (or 159.7 cm). On the other hand, teen boys at the age of 15 have a much higher average height, which is 67.0 inches (or 170.1 cm).
Can you grow between 16 and 18?
Most girls stop growing taller by age 14 or 15. However, after their early teenage growth spurt, boys continue gaining height at a gradual pace until around 18. Note that some kids will stop growing earlier and others may keep growing a year or two more.
Can you grow 1 cm after 17?
Even with a healthy diet, most people's height won't increase after age 18 to 20. The graph below shows the rate of growth from birth to age 20. As you can see, the growth lines fall to zero between ages 18 and 20 ( 7 , 8 ). The reason why your height stops increasing is your bones, specifically your growth plates.