CS 111 › Lesson 11 of 12

Inheritance & Polymorphism

Lesson 11 · CS 111: Programming I — Python · OKSTEM College

Inheritance & Polymorphism

Inheritance lets you build new classes on top of existing ones, reusing and extending their behavior.

Work through the concepts using Python in your editor or Replit. Use the knowledge check below to test yourself.

Try It Yourself

Key Exercise

Open a Python file and implement the core concepts from this lesson. Start small, test each part, then combine.

Knowledge Check

What does inheritance allow?

Inheritance is about behavior, not memory sharing.
Correct.
That is just how Python works - no inheritance needed.
Any class can create multiple instances without inheritance.
Quick Recap

Inheritance lets a subclass reuse code from a parent class. Memory sharing is a separate concept.

Quick Recap

Multiple classes in one file is always allowed. Inheritance is about sharing behavior between parent and child classes.

Quick Recap

Instantiation and inheritance are independent features.

What does super().__init__() do?

super() provides access, not a copy.
Correct.
super() calls the parent, it doesn't replace it.
Import and super() are unrelated.
Quick Recap

super().__init__() calls the parent class's __init__ method to initialize the parent's portion of the object.

Quick Recap

super() provides a proxy to the parent class so you can call its methods. The parent class still exists.

Quick Recap

You import with 'from file import ClassName'. super() is for calling parent class methods.

Polymorphism means:

That is the opposite - polymorphism works with inheritance.
Correct.
Classes can have any number of methods.
That is dynamic typing, not polymorphism.
Quick Recap

Polymorphism: different classes implement the same interface/method name and can be used interchangeably.

Quick Recap

Polymorphism is about different classes sharing an interface. No limit on methods per class.

Quick Recap

Dynamic typing lets variables hold any type. Polymorphism is about objects of different types responding to the same method calls.

What is duck typing?

Duck typing is the opposite - don't check the type.
Correct - 'If it quacks like a duck, it's a duck.'
Abstract classes use abc module, not duck typing.
Method overriding is what that is called.
Quick Recap

Duck typing: if it has the method you need, use it. Don't check isinstance() unless necessary.

Quick Recap

Abstract classes enforce that subclasses implement certain methods. Duck typing just uses whatever methods are available.

Quick Recap

Overriding means replacing a parent method in a subclass. Duck typing is about using objects based on their capabilities, not their type.

What is method overriding?

That would cause the second to silently replace the first.
Correct.
That raises TypeError.
Private methods use underscore prefix: _method.
Quick Recap

In Python, defining two methods with the same name in one class means the second replaces the first - not overriding in the OOP sense.

Quick Recap

Too many arguments raises TypeError. Overriding means redefining a parent method in a subclass.

Quick Recap

Private methods are prefixed with _ or __. Overriding is specifically about subclasses replacing parent methods.