Computer Science
Grade 9
20 min
Method Overriding: Customizing Inherited Behavior
Understand how to override methods in subclasses to provide specialized behavior.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define method overriding and explain its purpose in object-oriented programming.
Identify the parent (superclass) and child (subclass) in an inheritance relationship.
Implement method overriding in a child class to provide a specific version of a parent class's method.
Use the 'super' keyword to call the parent class's method from within the overridden method.
Explain how method overriding enables polymorphism.
Differentiate between method overriding and method overloading.
Have you ever noticed how a dog barks and a cat meows? πΆπ± They both 'make a sound', but they do it in their own special way. Method overriding is how we teach our code to do the same thing!
In this tutorial, you will learn how to take a general method f...
2
Key Concepts & Vocabulary
TermDefinitionExample
InheritanceThe process where one class (the child class) acquires the properties and methods of another class (the parent class). It's the foundation for an 'is-a' relationship (e.g., a Dog 'is-a' type of Animal).A `Car` class can inherit from a `Vehicle` class, automatically getting properties like `speed` and methods like `move()`.
Superclass (Parent Class)The class whose features are inherited. It's the more general or base class.In the relationship between `Animal` and `Dog`, the `Animal` class is the superclass.
Subclass (Child Class)The class that inherits from another class. It's the more specific or derived class.In the relationship between `Animal` and `Dog`, the `Dog` class is the subclass.
Method SignatureThe unique ident...
3
Core Syntax & Patterns
Overriding Syntax
class SubclassName(SuperclassName):
def method_to_override(self, parameters):
# New, specific code for the subclass
pass
To override a method, define a method in the subclass that has the exact same name and parameters as the method in the superclass. The code inside this method will replace the parent's version when called on a subclass object.
Using the 'super()' Keyword
class SubclassName(SuperclassName):
def method_to_override(self, parameters):
super().method_to_override(parameters)
# Add more specific code here
Use `super().method_name()` inside an overridden method to call the parent class's version of that same method. This is useful when you want to extend the parent's behavior, not j...
1 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You are designing classes for a drawing app with a `Shape` superclass. Why is using method overriding for a `draw()` method in subclasses (`Circle`, `Rectangle`) better than a single `draw()` method in `Shape` that uses `if/elif/else` to check the object's type?
A.It uses less memory because the code is split into more files.
B.It makes the code more extensible; adding a new `Triangle` shape doesn't require changing the existing `Shape` class's code.
C.It runs faster because it avoids `if` statements.
D.It is the only way to make the code work; `if` statements cannot check an object's type.
Challenging
What is the exact output of this code?
class Animal:
def speak(self): print('?')
class Dog(Animal):
def speak(self): print('Bark')
class Cow(Animal):
def speak(self): print('Moo')
class Cat(Animal):
pass
zoo = [Dog(), Cow(), Cat(), Animal()]
for animal in zoo:
animal.speak()
A.Bark
Moo
?
?
B.Bark
Moo
(no output)
?
C.Bark
Moo
Bark
Moo
D.An error will occur because Cat has no speak method.
Challenging
If a programming language supported inheritance but NOT method overriding, what would be the most significant negative consequence for software design?
A.Code could not be organized into classes.
B.Child classes could not have their own unique methods.
C.Polymorphism would be impossible to achieve, forcing developers to use `if/else` checks on object types, making code rigid and hard to maintain.
D.The `super()` keyword would not exist.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free