If your class hierarchy looks like a wildlife documentary gone wrong you are probably violating the Liskov Substitution Principle. That is the fancy rule that says a subclass must behave like its parent so callers do not get unwelcome surprises. Behold the sad truth of broken polymorphism and then learn how to stop the madness.
In plain programming language the rule is simple. If code expects a base type it should be able to use any subtype without changing correctness. If a subclass refuses to do what callers rely on you have a design bug not a mysterious runtime mood swing.
class Bird
def fly
return "flying"
end
end
class Penguin < Bird
def fly
raise "CannotFlyException"
end
end
Forcing a penguin to inherit flight is a design smell. Tests will fail, developers will grumble, and maintainability will take a long nap.
The goal is to align types with real capabilities not hopeful inheritance. Here are practical edits that actually help.
Instead of forcing every bird to have fly you might introduce a separate role object or interface for flying. Something like this in pseudocode keeps responsibilities clear.
class Bird
def speak
return "chirp"
end
end
module Flyable
def fly
return "flying"
end
end
class Sparrow < Bird
include Flyable
end
class Penguin < Bird
# no Flyable included
end
One killer habit is to add a set of contract tests that every subtype must pass. That way a future refactor that breaks a contract trips an alarm instead of causing a production nightmare.
def test_base_contract(obj)
# ensure methods that callers depend on behave consistently
assert obj.respond_to?(:speak)
# if object is supposed to fly then test flying behavior
if obj.respond_to?(:fly)
assert_equal "flying", obj.fly
end
end
Follow these rules and your hierarchies will stop telling lies. LSP is not just a rule to recite at code review it is the guardrail that keeps polymorphism useful and predictable. Write code that keeps promises and you will spend less time debugging and more time pretending you knew what you were doing all along.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.