Computer Science
Grade 8
20 min
Object-Oriented Concepts
Object-Oriented Concepts
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define inheritance and explain the 'is-a' relationship between classes.
Create a subclass that inherits attributes and methods from a parent class.
Override a method from a parent class to provide specialized behavior in a subclass.
Define polymorphism and explain how it allows different objects to respond to the same message.
Write code that uses a list of different objects and calls a common method on each.
Explain the concept of encapsulation as bundling data and methods together.
Ever wonder how a video game can have a hundred different characters (wizards, warriors, archers) that all know how to `attack()`, but each one does it in its own unique way? 🧙⚔️🏹
In this lesson, we'll explore the superpowers of Object-Oriented Programming!...
2
Key Concepts & Vocabulary
TermDefinitionExample
InheritanceThe ability of one class (the child or subclass) to get, or 'inherit', properties and methods from another class (the parent or superclass). It creates an 'is-a' relationship.A `Dog` class can inherit from an `Animal` class. A `Dog` *is an* `Animal`, so it inherits traits like `name` and methods like `eat()`.
PolymorphismFrom Greek, meaning 'many forms'. It's the ability of different objects to respond to the exact same method call in their own unique ways.If a `Dog` object and a `Cat` object both have a `make_sound()` method, calling it on the dog prints 'Woof!' and calling it on the cat prints 'Meow!'.
Method OverridingWhen a subclass provides its own specific version of a method that is already defi...
3
Core Syntax & Patterns
Inheritance Syntax
class SubclassName(SuperclassName):
Use this syntax to declare that a new class (the subclass) inherits from an existing class (the superclass). The subclass automatically gets all the non-private attributes and methods of the superclass.
Method Overriding Pattern
def method_name(self, parameters):
# New, specific code for the subclass
To override a method, simply define a method in the subclass that has the exact same name and parameters as the method in the superclass. The subclass's version will be called for its objects.
Using `super()`
super().method_name(parameters)
Inside a subclass's method, you can use `super()` to call the parent class's version of that same method. This is useful when you want to add to the parent'...
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
Challenging
A classmate designs a `Car` class that inherits from an `Engine` class (`class Car(Engine):`). Why is this a poor design choice according to the principles taught in the tutorial?
A.It violates the 'is-a' relationship; a Car is not an Engine, it 'has an' Engine.
B.An `Engine` class is too complex to be a parent class.
C.`Car` should always be a parent class, never a child class.
D.Inheritance only works for living things, like Animals.
Challenging
You are modeling a media player. You have items like `Song`, `Podcast`, and `Audiobook`. They all have a `title` and a `duration`, and they can all be `play()`ed. What is the best way to structure these classes using inheritance?
A.Create a `Song` class, and have `Podcast` and `Audiobook` inherit from `Song`.
B.Create a general `PlayableMedia` parent class with `title`, `duration`, and `play()`, and have `Song`, `Podcast`, and `Audiobook` inherit from it.
C.Create three separate classes with no inheritance, and just copy the `title`, `duration`, and `play()` code into each one.
D.Create a `Playlist` class and have `Song`, `Podcast`, and `Audiobook` inherit from it.
Challenging
Imagine a three-level inheritance: `class Shape:`, `class Polygon(Shape):`, and `class Square(Polygon):`. Each class has its own `draw()` method. If the `draw()` method in the `Square` class contains the line `super().draw()`, which `draw()` method will be executed by that line?
A.The `Shape` class's `draw()` method.
B.The `Square` class's own `draw()` method, causing an infinite loop.
C.All three `draw()` methods, starting from `Shape`.
D.The `Polygon` class's `draw()` method.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free