- LPU MCA Semester 1
- CAB106
- Unit 3
CAB106 · Unit 3: Encapsulation, inheritance and polymorphism
Method overriding and method overloading concept
10 practice questions
Key points
- Overriding: a child redefines a parent method
- super() lets an override extend the parent version
- Python has no true overloading; the last def wins
- Defaults and *args imitate overloading
Overriding means a child class defines a method with the same name as one in its parent, so objects of the child use the new version. The child can still reach the parent's version with super().method() and extend it. Calls such as self.name() inside a parent method are resolved on the object's real class, so an override in a child is used even when the call is written in the parent (dynamic dispatch).
Overloading means several methods sharing one name but differing in parameters. Python does not support it directly: a class body binds each name once, so a later def f replaces an earlier def f. Calling the removed version raises TypeError. Flexibility is achieved by other means:
| Technique | Example |
|---|---|
| Default arguments | def add(a, b=0, c=0) |
| Variable arguments | def area(*args) |
| Type checks | isinstance inside the method |
Overriding is a relationship between a parent and a child class and resolved at run time; overloading is a feature of languages such as Java and C++ that choose a version at compile time from the argument list.
Exam trap: defining f(self, a) and then f(self, a, b) does not give two methods. Only the last definition remains.
Try two questions
Redefining a parent class method with the same name in a child class is called
- Method overloading
- Method decoration
- Method binding
- Method overriding
Show the answer
D. Overriding replaces the inherited behaviour with the child's own version.
Why not A: Overloading means several versions with different parameters.
Why not B: Decoration wraps a function with another function.
Why not C: Binding ties a method to an object.
Which statement about method overloading in Python is correct?
- Python selects the version whose parameter count matches the call
- Python supports it only inside classes that inherit from object
- Python supports it only when each version has a different return type
- It is not supported directly, because the last definition of a name replaces earlier ones
Show the answer
D. Python keeps one function per name; flexibility comes from defaults, *args or type checks.
Why not A: That is how Java and C++ behave, not Python.
Why not B: Every Python class inherits from object, and overloading is still not built in.
Why not C: Return types play no part in how Python chooses a function.