Computer Science Grade 8 20 min

Classes and Objects

Classes and Objects

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define and explain the concept of inheritance. Create a subclass that inherits properties and methods from a superclass. Override a method from a superclass to give a subclass unique behavior. Use the `super()` function to call a method from the parent class. Differentiate between class attributes (shared by all objects) and instance attributes (unique to each object). Design a simple class hierarchy to model a real-world relationship. Ever noticed how a 'Poodle' is a special type of 'Dog', and a 'Dog' is a special type of 'Animal'? 🐾 How can we write code that shows these 'is-a' relationships? In this lesson, we'll explore advanced topics for classes and objects, focusing on inheritance. You'l...
2

Key Concepts & Vocabulary

TermDefinitionExample InheritanceThe process where one class, called the subclass, takes on the attributes and methods of another class, called the superclass.A `Car` class can inherit from a `Vehicle` class. The `Car` automatically gets the `Vehicle`'s attributes like `speed` and methods like `move()`. Superclass (Parent Class)The class that is being inherited from. It is the more general or base class.In the relationship between `Vehicle` and `Car`, `Vehicle` is the superclass. Subclass (Child Class)The class that inherits from a superclass. It is the more specific or specialized class.In the relationship between `Vehicle` and `Car`, `Car` is the subclass. Method OverridingWhen a subclass provides its own unique implementation of a method that is already defined in its superclass.I...
3

Core Syntax & Patterns

Inheritance Syntax class SubclassName(SuperclassName): To create a subclass, write the name of the superclass inside parentheses after the subclass's name. This establishes the parent-child relationship. Method Overriding Pattern def method_name(self, parameters): # New 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 now be used for its objects. Calling Parent Constructor super().__init__(parent_parameters) Inside the subclass's `__init__` method, this is the first line you should write. It ensures that all the setup from the parent class's `__init__` method is run before you add the subclass's specif...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A `Character`'s `attack` method reduces a target's health by 10. The `Wizard` subclass has a `magic_boost` attribute. How could the `Wizard` override `attack` to deal double damage if `magic_boost` is true, using the parent's logic?
A.def attack(self, target): target.health -= 20
B.def attack(self, target): super().attack(target); if self.magic_boost: super().attack(target)
C.def attack(self, target): super().attack(target); super().attack(target)
D.def attack(self, target): return 20
Challenging
Given `class A: x = 10` and `class B(A): pass`. Then we run `b1 = B(); b2 = B(); A.x = 20`. What is the value of `b1.x`?
A.10
B.An error occurs.
C.20
D.None
Challenging
You have a superclass `Shape` with an `area()` method that returns 0. A subclass `Square` is created with a `side_length` attribute. How should the `Square` class implement its own `area()` method?
A.It should not have an `area()` method and just use the one from `Shape`.
B.def area(self): return self.side_length * self.side_length
C.def area(self): return super().area() * 4
D.def area(self): return 0

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.