79836562

Date: 2025-12-03 05:57:29
Score: 1
Natty:
Report link

Overriding a method and defining a new function may look similar, but they are used for completely different reasons.

1. When you override a method

You're basically saying:

“This method already exists in the parent class, but I want to change how it behaves in my new class.”

You keep the same method name, but you give it new behavior.

This is useful when you want different classes to have the same function name but behave differently.
For example:

This is part of polymorphism, which is a core idea in OOP.


2. When you define a new function

You're adding brand-new behavior that didn’t exist before.

You are basically saying:

“The parent class didn’t have this function, so I’m creating it for my class.”

This function has no connection to anything in the parent class.


So what’s the difference?

Even though the syntax looks similar, the purpose is totally different.

That’s why overriding exists — it lets different classes share the same method name but behave differently, which makes code cleaner and easier to manage.


Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Anmol Yadav