Computer Science
Grade 9
20 min
Inheritance Basics
Inheritance Basics
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define polymorphism and method overriding in the context of inheritance.
Explain the 'is-a' relationship between a superclass and a subclass.
Implement a simple class hierarchy with one superclass and at least two subclasses.
Use the `super()` keyword to call a parent class's constructor from a child class.
Override a method from a parent class to provide specialized behavior in a child class.
Differentiate between a base class (parent) and a derived class (child).
Ever wonder how a video game can have a general 'Enemy' class, but also specific 'Goblin' and 'Dragon' enemies that attack differently? 🧙♂️🐉
This lesson explores how inheritance allows us to build specialized classes from general ones. You'l...
2
Key Concepts & Vocabulary
TermDefinitionExample
Superclass (Parent/Base Class)The general class that other classes inherit from. It contains common attributes and methods that are shared by its children.An `Animal` class could be a superclass for `Dog` and `Cat`.
Subclass (Child/Derived Class)The specialized class that inherits attributes and methods from a superclass. It can also have its own unique properties and behaviors.A `Dog` class is a subclass of the `Animal` class. It inherits properties like `name` but might have a unique method like `bark()`.
'is-a' RelationshipThe fundamental principle of inheritance. A subclass 'is-a' more specific type of its superclass.A `Car` 'is-a' `Vehicle`. A `Square` 'is-a' `Shape`. This helps you decide when to use inheritance.
Method O...
3
Core Syntax & Patterns
Inheritance Syntax
class SubclassName(SuperclassName):
To make one class inherit from another, you place the superclass's name in parentheses after the subclass's name during its definition.
Calling the Parent Constructor
super().__init__(parent_arguments)
Inside the subclass's `__init__` method, this is the first line you should write. It ensures that all the setup from the parent class is completed before adding the subclass's specific attributes.
Method Overriding Pattern
def method_name(self, parameters):
# New, specialized code here
To override a method, simply define a method in the subclass with the exact same name and parameters as the one in the superclass. The subclass's version will be used for its objects.
4 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
Easy
In the context of inheritance, what is a superclass?
A.class that inherits from another class.
B.general class that other classes inherit from.
C.class that cannot be inherited from.
D.specialized instance of another class.
Easy
In the context of inheritance, what is the term for the general class that other, more specialized classes inherit from?
A.Subclass
B.Superclass
C.Metaclass
D.Derived Class
Easy
The fundamental principle of inheritance, which states that a subclass is a more specific type of its superclass, is known as what?
A.'has-a' relationship
B.'uses-a' relationship
C.An 'is-a' relationship
D.'makes-a' relationship
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free