Method overriding is when a child uses its own special version of something that was already used by its parent.
Imagine you and your friend both have a toy robot. Your robot says "Hello!" when it starts up. But your friend’s robot has been upgraded, it now says "Hi there!" instead. Even though both robots do the same job (greeting), your friend's version is different.
This is like method overriding in programming. The parent class (like your robot) has a method that does something, and the child class (like your friend’s robot) changes it to work differently, but keeps the same name.
How It Works in Real Life
Think of it like this:
- Your mom makes chocolate cake every Sunday.
- One day, you decide to make vanilla cake instead.
You're still making a cake (same task), but your version is different.
In coding terms:
- Mom's
makeCake()method = chocolate cake - Your
makeCake()method = vanilla cake
The name of the action is the same, but the way it’s done changes, just like in method overriding!
Examples
- A dog can bark, but a puppy can whine. Both are dogs, but they have different ways of making noise.
- A car drives, but a sports car zooms. Both are cars, but they move differently.
- A chef cooks, but a pastry chef bakes. Both are chefs, but their speciality differs.
Ask a question
See also
- How Does Genetics 101 | National Geographic Work?
- How Does Billionaire Leaves His Daughter A Mysterious Bunker As Inheritance Work?
- How Does Will vs Trust in 10 Minutes or Less (Attorneys Explain) Work?
- Mom vs. Dad: What Did You Inherit?
- How Eye Color Is Actually Determined?