Interview Questions: Inheritance & Polymorphism
Inheritance and polymorphism are where OOP interview questions get genuinely conceptual, not just syntax recall. This set covers what a subclass actually inherits "for free" versus what still has to be written, why forgetting super().__init__() silently breaks things, the real difference between overriding and overloading (Python doesn't have true method overloading), what polymorphism actually buys a program with a concrete example, isinstance() vs. type() and why isinstance() respects subclassing, duck typing as a related but distinct idea, and one of the most common real interview questions on this topic: when composition beats inheritance.
1 When a class inherits from another class, what does it actually get "for free," and what still has to be written by hand?
A subclass automatically gets every attribute and method the parent class defines, without retyping any of that code — if Vehicle defines describe() and calculate_rental_cost(), a new class Car(Vehicle): can call my_car.describe() immediately, even before Car defines a single line of its own, simply because Python looks up an attribute or method on the instance's own class first and then walks up to its parent if it isn't found there. What still has to be written by hand is anything genuinely specific to the subclass: its own __init__ if it needs extra attributes the parent doesn't have (like num_doors on a Car), any method it wants to override with different behavior, and any brand-new method that only makes sense for that subclass and isn't part of the shared parent interface at all. Inheritance is a shortcut for shared structure and behavior — it doesn't write subclass-specific logic for you.
Believing a subclass has to redeclare every parent method just to "have" it, or not being able to name anything a subclass still has to write itself.
Follow-up"If Car(Vehicle) defines no __init__ of its own at all, and code does Car("Toyota", "Camry", 55), whose __init__ actually runs?" (Vehicle.__init__ — if a subclass doesn't define its own __init__, Python walks up to the parent's and uses that one instead, exactly like any other inherited method.)
2 What does super().__init__() actually do, and why does it matter?
super() gives access to the parent class from inside a subclass, and super().__init__(...) specifically calls the parent's __init__ method — running whatever setup logic the parent already defines (typically setting up its own instance attributes) before or alongside the subclass's own setup. This matters because a subclass's __init__ almost always needs to do two things: everything the parent already knows how to do (set up shared attributes like make, model, daily_rate), plus something extra specific to the subclass (like num_doors on a Car). Calling super().__init__(make, model, daily_rate) reuses the parent's existing setup code instead of copy-pasting it into every subclass, which is exactly the point of inheritance in the first place — write the shared setup once, in one place, and every subclass calls it rather than duplicating it.
Thinking super() refers to the subclass itself rather than the parent, or not recognizing that skipping it just means the parent's setup logic simply never runs.
"Inside Car.__init__(self, make, model, daily_rate, num_doors), what's the typical first line, and why first?" (super().__init__(make, model, daily_rate), typically first, so the shared attributes the parent sets up exist before the subclass's own __init__ body goes on to set self.num_doors or do anything else that might depend on the parent's setup already being done.)
3 A subclass defines its own __init__ but forgets to call super().__init__(). What actually breaks?
Nothing crashes immediately at class-definition time, which is exactly why this bug is sneaky — the program runs, the object gets created, and the crash only shows up later, whenever code tries to use an attribute the parent's __init__ was supposed to set up. Concretely: if Vehicle.__init__ sets self.make, self.model, and self.daily_rate, but Car.__init__ only sets self.num_doors and never calls super().__init__(...), then a newly created Car instance simply has no make, model, or daily_rate attribute at all. The moment any code tries my_car.make or calls my_car.describe() (if describe() reads self.make), it raises an AttributeError, often far away from and much later than where the actual mistake was made, which makes it a genuinely realistic bug to hunt down in a real codebase.
Claiming Python automatically calls the parent's __init__ even if the subclass defines its own without calling super().__init__() (it does not — defining any __init__ in the subclass completely replaces the parent's, unless super().__init__() is called explicitly).
"If Car defines no __init__ at all (not even one that forgets super()), does the same bug happen?" (No — if the subclass defines zero __init__ methods, Python uses the parent's automatically, exactly as normal inheritance lookup works; the bug only appears once the subclass writes its own __init__ and forgets the super() call inside it.)
4 What's the actual difference between method overriding and method overloading? Does Python support both?
Overriding is when a subclass redefines a method that already exists on its parent class, giving that same method name different behavior for that subclass — e.g. Motorcycle.calculate_rental_cost() applying a multi-day discount that Vehicle.calculate_rental_cost() doesn't. Python fully supports this, and it's one of the core tools of this chapter. Overloading, in languages that support it (like Java or C++), means defining multiple versions of the same method name in the same class, distinguished by how many arguments they take or what types those arguments are, and the language picks which version to run based on the call site. Python does not support true method overloading in that sense — there's no way to define two separate methods both named area in the same class, one taking one argument and one taking two, and have Python automatically pick the right one; the second definition simply replaces the first, since a class body is just executed top to bottom and a later def with the same name overwrites the earlier one. Python programs approximate what other languages use overloading for with default arguments, *args/**kwargs, or checking argument types manually inside a single method — but that's a workaround pattern, not language-level overloading.
Confusing overriding with overloading (a very common mix-up), or claiming Python supports true method overloading the way Java does.
Follow-up"If a class body defines def greet(self): ... and then, further down in the same class body, another def greet(self, name): ..., what happens when instance.greet() is called with no arguments?" (It raises a TypeError for a missing required argument — only the second greet definition exists at all, since it silently replaced the first one; Python never keeps both versions around to choose between.)
5 When a subclass overrides a method, how does it still reuse the parent's original version instead of throwing it away entirely?
By calling super().method_name(...) from inside the override, which invokes the parent class's original version of that method directly, and then the subclass's override can add its own behavior before or after that call. This is the same super() mechanism used for __init__, just applied to any other method — for example, Car.describe(self) could call base = super().describe() to get the parent's plain "Toyota Camry" string, then return f"{base} (Car, {self.num_doors} doors)", extending rather than fully replacing the parent's behavior. This is a genuinely common and useful pattern: it avoids duplicating whatever logic the parent's version already has correct, while still letting the subclass add exactly the extra detail it needs on top.
Thinking overriding a method means the parent's version is permanently inaccessible from the subclass, or not knowing super().method_name() is how to reach it deliberately.
"Inside Motorcycle.calculate_rental_cost(self, days), what's a natural way to reuse Vehicle's plain daily_rate * days calculation before applying an extra discount?" (Start with cost = super().calculate_rental_cost(days) to get the parent's base calculation, then apply the discount to that result, e.g. cost *= 0.9 if days >= 7, rather than re-writing self.daily_rate * days from scratch.)
6 What does "polymorphism" actually mean in practice? Give a concrete example.
Polymorphism means calling the exact same method name on different objects and getting behavior appropriate to each object's actual type, without the calling code needing to know or check which specific type it's dealing with. Concretely: given a list mixing Car and Motorcycle objects (both subclasses of Vehicle), a loop like for v in fleet: print(v.describe()) calls describe() the same way on every single object, but each one runs its own overridden version — a Car prints something mentioning doors, a Motorcycle prints something mentioning a sidecar — and the loop itself never has to ask "is this a Car or a Motorcycle?" to decide what to do. The calling code is written once, against the shared Vehicle interface (every subclass has a describe() method), and stays correct automatically even if a brand-new subclass like Truck gets added later with its own describe() override — nothing about the loop needs to change to support it.
Defining polymorphism only abstractly ("many forms") without a concrete example of the same call producing genuinely different behavior per object, or confusing it with plain method overriding on its own without the "same call site, different objects" angle.
Follow-up"If a new Truck(Vehicle) subclass is added later with its own describe() override, does the existing for v in fleet: print(v.describe()) loop need to change to handle it?" (No — that's the entire benefit; the loop calls describe() polymorphically on whatever object it gets, so a new subclass with its own override slots in automatically with zero changes to the calling code.)
7 What's the difference between isinstance(obj, SomeClass) and type(obj) == SomeClass?
type(obj) == SomeClass checks for an exact type match only — it's True only if obj's type is literally SomeClass, and False for any subclass instance, even though a subclass instance genuinely "is a" SomeClass in every meaningful sense. isinstance(obj, SomeClass) checks the whole inheritance chain — it returns True if obj's type is SomeClass or any subclass of it. Concretely, given class Car(Vehicle): pass and my_car = Car(...): isinstance(my_car, Vehicle) is True (a Car is-a Vehicle), but type(my_car) == Vehicle is False (its exact type is Car, not Vehicle). This is why isinstance() is almost always the right choice in real code — it respects the whole point of inheritance (a subclass instance should be usable anywhere the parent type is expected), while type() == checking silently breaks the moment a new subclass is introduced.
Treating isinstance() and type() == as interchangeable, or not knowing which one respects subclassing.
"Given class Cat(Animal): pass and c = Cat('Tom'), what does isinstance(c, Animal) return, and what does type(c) == Animal return?" (isinstance(c, Animal) is True — Cat is a subclass of Animal, so a Cat instance genuinely is an Animal too; type(c) == Animal is False — c's exact type is Cat, not Animal.)
8 Why does most real-world Python code prefer isinstance() checks over type() == checks, when either is technically possible?
Because type() == silently stops working correctly the instant a new subclass is introduced, while isinstance() keeps working correctly automatically. Imagine code that does something like if type(v) == Vehicle: apply_standard_fee(v) — that check would be False for every Car and Motorcycle instance, even though they're conceptually vehicles too, purely because their exact type isn't literally Vehicle. Using isinstance(v, Vehicle) instead correctly includes every subclass, present and future, without the check needing to be updated every time a new subclass is added to the codebase. This matters a great deal in growing real systems: a codebase that relies on type() == checks tends to accumulate subtle bugs exactly at the moments new subclasses get introduced, since nothing forces anyone to go find and update every scattered exact-type check.
Not being able to articulate a concrete failure scenario for type() == checks, or claiming there's no real practical difference between the two in typical code.
"Is there ever a legitimate reason to want the stricter, exact-type-only behavior of type() == instead of isinstance()?" (Rarely, but yes — if code genuinely needs to distinguish the exact class from any of its subclasses, e.g. some serialization logic that must behave differently for the base class specifically; it's uncommon enough that isinstance() should be the default choice unless there's a specific, deliberate reason to want the stricter check.)
9 What is "duck typing," and how is it related to, but different from, polymorphism through inheritance?
Duck typing is the idea, named after "if it walks like a duck and quacks like a duck, it's a duck," that Python code often doesn't check an object's actual type or inheritance at all before using it — it just tries calling the method or attribute it needs, and if the object happens to have that method (whatever its actual class is), the code works, no shared parent class required. This is related to inheritance-based polymorphism — both let the same code work correctly across different types of objects — but the mechanism is different: inheritance-based polymorphism relies on a shared parent class defining a common method that every subclass overrides, while duck typing relies on nothing more than "this object happens to have a method with this name," with no formal relationship between the classes involved at all. A concrete example: a function def print_all(items): [print(i.describe()) for i in items] works fine on a list of unrelated classes that each just happen to define their own describe() method, even if none of them inherit from a common parent — that's duck typing; the Vehicle/Car/Motorcycle hierarchy in this chapter, where describe() is guaranteed by a shared parent class, is polymorphism through inheritance instead.
Treating duck typing and inheritance-based polymorphism as identical with no distinction, or being unable to give an example of duck typing that doesn't involve any shared parent class at all.
Follow-up"Does duck typing require any two classes involved to share a common parent class?" (No — that's precisely the distinction; duck typing only cares that an object has the method being called, regardless of its class hierarchy, while inheritance-based polymorphism specifically relies on a shared parent guaranteeing that method exists across all its subclasses.)
10 When is composition a better design choice than inheritance? Why is this a genuinely common real interview question?
Inheritance models an "is-a" relationship (a Car is a Vehicle), while composition — giving a class an attribute that's an instance of another class, rather than inheriting from it — models a "has-a" relationship (a Car has an Engine, rather than being one). Composition tends to be the better choice when the relationship genuinely isn't "is-a": it would be a design mistake to make Engine a parent class of Car just to reuse its methods, since a car conceptually contains an engine rather than being a specialized kind of engine. Composition is also generally more flexible than inheritance for changes that come up over time — swapping out one Engine instance for a different one on an existing Car object is straightforward, whereas deep, multi-level inheritance hierarchies tend to get rigid and fragile as requirements evolve (a change to a base class ripples through every subclass beneath it, sometimes in surprising ways). This is a genuinely common real interview question because a huge amount of practical, maintainable software design comes down to correctly recognizing "is-a" versus "has-a" relationships, and overusing inheritance where composition would fit better is one of the most common design mistakes newer developers make.
Reaching for inheritance by default for every relationship without considering whether it's genuinely "is-a," or being unable to give a concrete example where composition is clearly the better fit.
Follow-up"Would it make sense for a RentalAgreement class to inherit from Vehicle, or should it hold a Vehicle as an attribute instead?" (It should hold a Vehicle as an attribute — a rental agreement has a vehicle it refers to, it isn't itself a specialized kind of vehicle, so composition is the correct relationship here, not inheritance.)
11 A method loops over a list of objects and uses if type(v) == Car: ... elif type(v) == Motorcycle: ... to decide what to print for each one. What's wrong with this, and how should it be rewritten?
This pattern — sometimes called a "type-check chain" — defeats the entire purpose of having a shared parent class and overridden methods in the first place. Every time a new subclass (say, Truck) gets added, this if/elif chain has to be found and manually updated with a new branch, or the new subclass's objects silently fall through with no matching case at all — the exact opposite of polymorphism's promise that new subclasses slot in automatically. The fix is to give every subclass its own overridden version of the same method (e.g. describe()) and simply call it polymorphically: for v in fleet: print(v.describe()), with no type-checking at all. Each object already knows how to describe itself correctly because of its own class's override — the calling code doesn't need to, and shouldn't need to, ask what type it's looking at.
Not recognizing a type-check chain as an anti-pattern, or "fixing" it by just adding another elif branch instead of pushing the type-specific logic into an overridden method on each subclass.
"If a new Truck(Vehicle) subclass is added, what has to change about a polymorphic for v in fleet: print(v.describe()) loop versus a type(v) == ... chain?" (The polymorphic loop needs zero changes at all — it works automatically as long as Truck defines its own describe(); the type-check chain needs a brand-new elif type(v) == Truck: branch added by hand, or Truck objects get silently skipped.)
12 Does a subclass have to override every method its parent defines?
No — a subclass overrides only the methods where it genuinely needs different behavior, and simply inherits every other method from the parent completely unchanged. In the Vehicle/Car/Motorcycle example, both subclasses might override describe() to mention their own specific details, but if the base Vehicle also defined something like is_available(self) that works identically for every vehicle type regardless of subclass, neither Car nor Motorcycle would need to touch it at all — calling my_car.is_available() would just run Vehicle's original version directly, with no override in the way. This is exactly the point of inheritance: write shared behavior once on the parent, and only override the specific pieces that genuinely need to differ per subclass.
Assuming every subclass must redefine every single parent method to "be complete," which misunderstands what inheritance is for in the first place.
Follow-up"If Vehicle defines is_available() and neither Car nor Motorcycle overrides it, whose version runs when my_motorcycle.is_available() is called?" (Vehicle.is_available() — Python looks for the method on Motorcycle first, doesn't find an override there, and walks up to the parent class where it does find one, exactly like ordinary attribute/method inheritance.)
13 Can isinstance() check against more than one class at once? What's that useful for?
Yes — isinstance() accepts a tuple of classes as its second argument and returns True if the object is an instance of any of them, e.g. isinstance(value, (int, float)) is True for either an int or a float. This is useful any time code needs to accept more than one acceptable type without writing out multiple separate isinstance() checks joined by or. Applied to a class hierarchy, this could look like isinstance(v, (Car, Motorcycle)) to check "is this either of these two specific vehicle subclasses" without matching every possible Vehicle subclass in general — useful when logic genuinely needs to branch on a specific subset of subclasses rather than the whole family.
Not knowing isinstance() accepts a tuple at all, or writing out a long chain of isinstance(v, Car) or isinstance(v, Motorcycle) when a single tuple call does the same thing more cleanly.
"What does isinstance(5, (int, float)) return?" (True — 5 is an instance of int, which is one of the two types in the tuple, so the check succeeds without needing 5 to also be a float.)
14 Every Python class, even one with no explicit parent written, still inherits from something. What is it, and why does that matter?
Every class in Python implicitly inherits from object, the root of Python's entire class hierarchy, even when nothing is written after the class name like class Vehicle: with no parentheses at all — that's shorthand for class Vehicle(object):. This is why every ordinary object already has certain baseline behavior available before a single custom method is written — things like a default __repr__, a default __eq__ that compares by identity, and the ability to check isinstance(v, object), which is always True for literally any object, since object sits at the top of every class's inheritance chain. It matters practically because it explains why dunder methods like __str__ or __eq__ (covered in earlier chapters) "already exist" in some default form even on a brand-new class that never explicitly defined them — they're inherited from object, and overriding them is exactly the same override mechanism this chapter covers for any other method.
Believing a class with no explicit parent listed has no parent at all, or not connecting this to why default dunder method behavior already exists before a class defines its own.
Follow-up"Is issubclass(Vehicle, object) True or False, given class Vehicle: with no explicit parent written?" (True — every class, whether or not it lists an explicit parent, is automatically a subclass of object.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.