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)
In Python, inheritance is a powerful mechanism that allows you to define a new class that is a modified version of an existing class. Inheritance is used to create new classes that have the same attributes and methods as the parent class but with additional or modified functionality.
To use inheritance in Python, you define a new class that inherits from an existing class by using the class
keyword followed by the name of the new class and the name of the parent class in parentheses. Here's an example:
class Animal: def __init__(self, name): self.name = name def speak(self): print("Animal speaking...") class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed = breed def speak(self): print("Woof!") my_dog = Dog("Fido", "Labrador") print(my_dog.name) # Output: "Fido" print(my_dog.breed) # Output: "Labrador" my_dog.speak() # Output: "Woof!"
In this example, we define a class called Animal
with an __init__()
method and a speak()
method. We also define a new class called Dog
that inherits from the Animal
class. The Dog
class overrides the speak()
method to print "Woof!" and adds a new attribute breed
.
To create an instance of the Dog
class, we can call its constructor and pass in the name
and breed
arguments:
my_dog = Dog("Fido", "Labrador")
The super()
function is used to call the __init__()
method of the parent class Animal
, which initializes the name
attribute. The Dog
class then initializes the breed
attribute.
We can access the name
and breed
attributes of the my_dog
instance using dot notation:
print(my_dog.name) # Output: "Fido" print(my_dog.breed) # Output: "Labrador"
We can also call the speak()
method of the my_dog
instance, which is overridden in the Dog
class:
my_dog.speak() # Output: "Woof!"
In summary, inheritance is a powerful mechanism that allows you to define a new class that is a modified version of an existing class. In Python, you can use inheritance to create new classes that inherit attributes and methods from a parent class and add new functionality or modify existing functionality.
Applying inheritance to improve code reusability in Python:
class Shape: def draw(self): pass class Circle(Shape): def draw(self): print("Drawing a circle") class Square(Shape): def draw(self): print("Drawing a square")
Inheriting from built-in classes in Python:
class CustomList(list): def add_element(self, element): self.append(element) custom_list = CustomList([1, 2, 3]) custom_list.add_element(4) print(custom_list) # Output: [1, 2, 3, 4]
Creating and utilizing abstract base classes in Python:
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def draw(self): pass class Circle(Shape): def draw(self): print("Drawing a circle") class Square(Shape): def draw(self): print("Drawing a square")
Polymorphism and method overriding with Python inheritance:
class Animal: def speak(self): pass class Dog(Animal): def speak(self): print("Dog barks") class Cat(Animal): def speak(self): print("Cat meows") def animal_sound(animal): animal.speak() my_dog = Dog() my_cat = Cat() animal_sound(my_dog) # Output: Dog barks animal_sound(my_cat) # Output: Cat meows