HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Going Further
  3. /Inheritance and Special Methods

Inheritance and Special Methods

Python Programming · High School (11–12) · 1 course, 7 worked problems, 53 practice questions

Inheritance lets a new class reuse an old one. A class Dog(Animal) gets all the attributes and methods of Animal, and it can add new ones or override old ones. The function super() calls the parent's version of a method. Special methods have names that start and end with two underscores, and they let your objects work with Python's own syntax. __str__ sets what print() shows, __add__ lets you use +, and __eq__ decides when two objects are equal. With these, a Vector class can add and print vectors like numbers.

With practice you will be able to

  • Write a subclass that inherits and overrides methods
  • Use super() to call a method of the parent class
  • Add __str__ and __add__ methods to a class

On the syllabus of

  • Python Certification
  • IB
  • A-Level

Lessons

  • Inheritance and Special MethodsIs a car a kind of engine, or does it have one? How does print show your own object?High School (11–12)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A subclass adds a method but no constructor of its own. class Item: def __init__(self, name, price): self.name = name self.price = price class Book(Item): def label(self): return self.name.upper() b = Book("atlas", 300) print(b.label(), b.price) c = Book("map") What happens when the program runs?

A class inherits from two parents. Both parents have a constructor and a method called start. class Engine: def __init__(self): self.power = 100 def start(self): return "engine" class Radio: def __init__(self): self.volume = 5 def start(self): return "radio" class Car(Engine, Radio): pass c = Car() print(c.start(), hasattr(c, "power"), hasattr(c, "volume")) What does the program print?

The constructor of a parent class calls a method that the child class overrides. class Widget: def __init__(self): self.parts = [] self.setup() def setup(self): self.parts.append("base") class Button(Widget): def __init__(self, text): super().__init__() self.text = text def setup(self): self.parts.append(self.text) b = Button("OK") print(b.parts) What happens when the program runs?

Three constructors in a chain each set or change the same attribute. class A: def __init__(self): self.trail = ["A"] class B(A): def __init__(self): self.trail = ["B"] super().__init__() class C(B): def __init__(self): super().__init__() self.trail.append("C") print(C().trail) What does the program print?

A subclass inherits __str__ from its parent, and a third class extends it with super(). class Animal: sound = "..." def __str__(self): return f"{type(self).__name__} says {self.sound}" class Cat(Animal): sound = "meow" class Kitten(Cat): def __str__(self): return "small " + super().__str__() print(Cat(), Kitten(), sep=" | ") What does the program print?

A base class has a method that only its subclasses should fill in. It raises NotImplementedError until they do. class Shape: def area(self): raise NotImplementedError def describe(self): return f"area {self.area()}" class Square(Shape): def __init__(self, s): self.s = s def area(self): return self.s * self.s class Blob(Shape): pass Each line below is run on its own, after these classes. Which of them run without raising an exception? Select all that apply.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A duck can both swim and fly, so its class lists two parents. class Swimmer: pass class Flyer: pass class Duck(Swimmer, Flyer): pass Which of these facts about the three classes come out True? Select all that apply.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A VIP ticket should cost 50 more than a normal ticket, so the program should print 150. It stops with RecursionError instead. class Ticket: def price(self): return 100 class Vip(Ticket): def price(self): return self.price() + 50 print(Vip().price()) Which one change makes it print 150?

In this program the line marked ____ is missing. The program should print Ira 30. class Named: def __init__(self, name): self.name = name class Aged: def __init__(self, age): self.age = age class Person(Named, Aged): def __init__(self, name, age): Named.__init__(self, name) ____ p = Person("Ira", 30) print(p.name, p.age) Which line, put in place of ____, makes the program print Ira 30?

Three classes are written first. Then each line below is added, on its own, after them. class A: pass class B(A): pass class C(A): pass Which of these lines run without raising an exception? Choose every correct option.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

All 53 practice questions on Inheritance and Special Methods →

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • Given these classes, say what each line shows.Easy
  • A Dog object has no name. Find the reason and fix the class.Medium
  • Who says what?Easy
  • The loop below works for any animal. Add a class Bird that says Tweet, and a class Parrot built on…Medium
  • For each line of code, name the special method that Python calls…Easy
  • A class Note has an attribute rupees. Write __lt__ and __str__ so that sorted puts a list of notes…Medium
  • For each pair, say whether you would use inheritance (is a) or composition (has a), and why…Easy

Python labs

  • A child class
  • Shapes with areas
  • Show a Point
  • A Vector class
  • A garage of cars
  • Project: a deck of cards

Related topics

  • Python Going Further
  • Classes and Objects
  • Files and Data Formats
  • Comprehensions, Iterators and Generators
  • Data Structures
  • Testing and Debugging