Method Overriding and Polymorphism
Learn what method overriding and polymorphism are and how we can use them.
Chirpy has come a long way—from simple users and posts to verified accounts with their own badges. Up to now, we’ve built a solid foundation. Users can:
Create posts
Like posts
Comment on posts
And we’ve even distinguished VerifiedUsers
by simply adding a new attribute.
However, there’s still one important requirement we haven’t met yet. Currently, verified users have their special badge—but that’s all. They still behave exactly like regular users, even though Chirpy’s project specification clearly states that verified users should have distinct behaviors and privileges beyond just a badge. For example, their posts might appear more prominently, or they might bypass certain restrictions that apply to regular users.
They can post, like, and comment in the same way. But in real-world social networks, verified users often have special privileges—maybe their posts get prioritized, they can bypass certain content checks, or they have a unique way of interacting with the platform.
How do we extend their functionality without changing how users interact with the system?
One way to handle this is by modifying our existing User
class with if
statements, like this:
Get hands-on with 1400+ tech skills courses.