Python Inheritance Tutorial
Inheritance is an Object-Oriented Programming (OOP) concept where a child class (subclass) inherits properties and methods from a parent class (superclass). This allows code reuse and better organization.
1. Creating a Basic Inheritance
A child class inherits all methods and attributes of a parent class.
Example: Basic Inheritance
class Animal:
def speak(self):
return "Animals make sounds"
class Dog(Animal): # Dog inherits from Animal
pass
dog = Dog()
print(dog.speak()) # Output: Animals make sounds
✅ The Dog
class inherits the speak()
method from Animal
.
2. Method Overriding
A child class can override a method of the parent class.
Example: Overriding Methods
class Animal:
def speak(self):
return "Animals make sounds"
class Dog(Animal):
def speak(self): # Overriding the method
return "Woof! Woof!"
dog = Dog()
print(dog.speak()) # Output: Woof! Woof!
✅ The speak()
method is redefined in the Dog
class.
3. Using the super()
Function
The super()
function calls a method from the parent class inside the child class.
Example: Using super()
to Extend Functionality
class Animal:
def speak(self):
return "Animals make sounds"
class Dog(Animal):
def speak(self):
parent_speak = super().speak() # Calls parent method
return parent_speak + " and Dogs bark!"
dog = Dog()
print(dog.speak())
# Output: Animals make sounds and Dogs bark!
✅ super()
helps call methods from the parent class inside the child class.
4. Inheriting the Constructor (__init__()
)
A child class can use the parent class constructor using super()
.
Example: Constructor Inheritance
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Calling parent constructor
self.breed = breed
dog = Dog("Buddy", "Labrador")
print(dog.name) # Output: Buddy
print(dog.breed) # Output: Labrador
✅ The Dog
class inherits the name
attribute from Animal
using super().__init__()
.
5. Multilevel Inheritance
A child class can be a parent to another child class.
Example: Multilevel Inheritance
class Animal:
def speak(self):
return "Animals make sounds"
class Dog(Animal):
def bark(self):
return "Dogs bark"
class Puppy(Dog):
def weep(self):
return "Puppies weep"
puppy = Puppy()
print(puppy.speak()) # Output: Animals make sounds
print(puppy.bark()) # Output: Dogs bark
print(puppy.weep()) # Output: Puppies weep
✅ The Puppy
class inherits from Dog
, which inherits from Animal
.
6. Multiple Inheritance
A child class can inherit from multiple parent classes.
Example: Multiple Inheritance
class Animal:
def speak(self):
return "Animals make sounds"
class Bird:
def fly(self):
return "Birds can fly"
class Parrot(Animal, Bird):
pass
parrot = Parrot()
print(parrot.speak()) # Output: Animals make sounds
print(parrot.fly()) # Output: Birds can fly
✅ Parrot
inherits from both Animal
and Bird
.
Conclusion
Inheritance allows code reusability and modularity, making it easier to build and maintain complex programs.
0 comments:
Post a Comment