Python Inheritance

1) Simple Inheritance

Define a base class Animal with a method sound() that prints "Some sound". Create a subclass Dog that inherits from Animal. Instantiate Dog and call sound().


2) Override Method

In Dog, override sound() to print "Woof!". Create object and call the method.


3) Subclass With New Method

Create base class Person with name. Subclass Student that adds a method study() printing "Studying".


4) Inherit Constructor

Have base class Vehicle with make and year. Subclass Car uses super() to call the base constructor.


5) Add Subclass Attribute

Base class Employee has name. Subclass Manager adds department. Print both attributes from an object.


6) Multilevel Inheritance

Create A → B → C (three levels). Each class has a simple method; call them from a C object.


7) Override with Super

In subclass Car, override a method and call super().method() inside it.


8) Inherit Without New Methods

Create class Shape; subclass Circle that just inherits without adding anything.


9) Check isinstance

Create object of Student and check:


10) Use issubclass

Check whether Student is a subclass of Person.


11) Multiple Subclasses

Base class Fruit; subclasses Apple, Banana, each with unique method.


12) Base Method Called from Subclass

Have base class method describe() that prints something; call it from subclass object.


13) Override Init

Subclass overrides __init__ and still uses super() to call the base __init__.


14) Extend Behavior

Base class method show() prints basic info. Subclass overrides it to show more fields.


15) Shared Method Across Classes

Classes Bird and Fish inherit common base Animal with move() method.


16) Class with Default Values

Base class has defaults; subclass uses defaults if no args are passed.


17) Dynamic Attributes in Subclass

Subclass adds attributes based on user input.


18) Inherit and Use List

Base class has a list attribute; subclass manipulates it.


19) Inherit and Validate

Subclass adds method that checks some condition (e.g., age ≥ 18) inherited from base.


20) Abstract-like Inheritance (Basic)

Create base class Base with a method that raises NotImplementedError. Subclass overrides it.


💡 Concepts Reinforced

Concept
What You Practice

Basic inheritance

Simple subclassing

Method overriding

Subclass custom behavior

super() use

Calling parent class constructor/method

Multilevel inheritance

Chain of classes

Type checking

isinstance, issubclass

Base class method reuse

Shared behavior


🧠 Example Starter

Base class & Subclass


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated