Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Inheritance is one of the fundamental principles of object-oriented programming (OOP). It allows you to create a new class that inherits the properties (attributes) and behaviors (methods) of an existing class. The new class is called a subclass or derived class, and the existing class is called the superclass or base class.
In this tutorial, we'll demonstrate how to use inheritance in Python to create a subclass that extends a superclass.
Example: Creating a Superclass
Let's create a superclass called Animal
that has a name
attribute and a speak()
method:
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound."
In this example, we define the Animal
superclass with a constructor that initializes the name
attribute and a speak()
method that returns a string representing the sound the animal makes.
Example: Creating a Subclass
Now, let's create a subclass called Dog
that inherits from the Animal
superclass and overrides the speak()
method:
class Dog(Animal): def speak(self): return f"{self.name} barks."
In this example, we create the Dog
subclass by specifying the Animal
superclass in parentheses after the class name. The Dog
subclass inherits the name
attribute and the speak()
method from the Animal
superclass but overrides the speak()
method to return a different string.
Example: Using the Subclass
Let's create instances of the Animal
superclass and the Dog
subclass and demonstrate the inheritance:
# Create an instance of the Animal superclass animal1 = Animal("Generic Animal") print(animal1.speak()) # Output: Generic Animal makes a sound. # Create an instance of the Dog subclass dog1 = Dog("Buddy") print(dog1.speak()) # Output: Buddy barks. # Check the isinstance relationship print(isinstance(dog1, Dog)) # Output: True print(isinstance(dog1, Animal)) # Output: True
In this example, we create instances of the Animal
superclass and the Dog
subclass and call their speak()
methods. We also use the isinstance()
function to check if the Dog
instance is an instance of both the Dog
subclass and the Animal
superclass.
In conclusion, inheritance in Python allows you to create new classes that extend existing classes by inheriting their attributes and methods. Inheritance promotes code reuse and makes it easier to model real-world relationships between different types of objects.
Defining and creating subclasses in Python:
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): pass my_dog = Dog() my_dog.speak() # Output: Animal speaks
Inheriting attributes and methods in Python:
class Animal: def __init__(self, name): self.name = name class Dog(Animal): pass my_dog = Dog("Buddy") print(my_dog.name) # Output: Buddy
Super() function and method resolution order in Python:
super()
is used to call methods from the superclass. Method resolution order (MRO) defines the sequence in which base classes are searched.class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): super().speak() print("Dog barks") my_dog = Dog() my_dog.speak()
Overriding methods in Python subclasses:
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") my_dog = Dog() my_dog.speak() # Output: Dog barks
Polymorphism and dynamic dispatch in Python:
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") def animal_sound(animal): animal.speak() my_animal = Animal() my_dog = Dog() animal_sound(my_animal) # Output: Animal speaks animal_sound(my_dog) # Output: Dog barks