🏗️ Classes & OOP

Classes & OOP: A class is like a car factory's BLUEPRINT — fields for color, speed, model are defined, but no one can ever drive the blueprint itself.

A class is like a car factory's BLUEPRINT — fields for color, speed, model are defined, but no one can ever drive the blueprint itself. The __init__ method is the job of filling in those fields for every car that rolls off the line: write TestRunner("Login Test", "Chrome") and Python effectively does "produce a new car, make its color Chrome." Here's a real question: why does Python raise an error if you give __init__ FEWER arguments than it expects? Because if the blueprint says "every car MUST have an engine," producing a car without one is meaningless — Java's constructors give the exact same guarantee, just under a different name. Class = blueprint, object = an actual car built from it, carrying its OWN specific data.

Java constructors are explicit: public Car(String color) {}. Python uses __init__ as the constructor. Python's 'self' is like Java's 'this' — but must be written explicitly in every method.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Java vs Python — Classes

What is the first parameter of a Python __init__ method?

'self' represents the current instance of the class — like Java's 'this'. It must be written explicitly in every instance method in Python.

What is the standard parameter name for the object itself in Python instance methods?

In Python, the first parameter of instance methods refers to the object itself and is conventionally named 'self'.

Inheritance is like a family passing down DNA — if ElectricCar inherits from Car, all of Car's properties (color, speed) automatically exist in ElectricCar too, with battery capacity added on top. But here's a trap worth thinking through: if Dog(Animal)'s __init__ FORGETS to call super().__init__(name), what happens? Animal's own __init__ never runs — meaning self.name is never set, and calling bark() later blows up with "AttributeError: no attribute 'name'". In Java, skipping a super() call either gets auto-handled (if a no-arg constructor exists) or flagged by the compiler — in Python it's SILENTLY skipped, and only explodes once that method actually gets called. So inheritance passes down properties, but it doesn't AUTOMATICALLY initialize anything — if you don't trigger it, the parent's setup simply never runs.

Java uses 'extends': class Dog extends Animal. Python puts the parent in parentheses: class Dog(Animal). Both languages use super() to call the parent constructor.

Step by Step: Python coding practice

Read inputs and data types

Complete the critical line with the smallest change