| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
Welcome to codeswithpankaj.com! In this tutorial, we will dive deep into the concept of inheritance in Python. We'll explore what inheritance is, its types, and how to implement it in Python with detailed explanations and examples.
Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit attributes and methods from another class. The class that inherits is called the derived class or child class, while the class being inherited from is called the base class or parent class.
Single inheritance involves a single base class and a single derived class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak()
dog.bark()Multiple inheritance involves multiple base classes and a single derived class.
class Mammal:
def has_fur(self):
print("Has fur")
class Bird:
def has_feathers(self):
print("Has feathers")
class Bat(Mammal, Bird):
def fly(self):
print("Can fly")
bat = Bat()
bat.has_fur()
bat.has_feathers()
bat.fly()Multilevel inheritance involves a chain of inheritance.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Puppy(Dog):
def weep(self):
print("Puppy weeps")
puppy = Puppy()
puppy.speak()
puppy.bark()
puppy.weep()Hierarchical inheritance involves multiple derived classes inheriting from a single base class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Cat(Animal):
def meow(self):
print("Cat meows")
dog = Dog()
cat = Cat()
dog.speak()
dog.bark()
cat.speak()
cat.meow()Hybrid inheritance is a combination of two or more types of inheritance.
class Engine:
def start(self):
print("Engine starts")
class ElectricEngine(Engine):
def charge(self):
print("Charging...")
class Car(Engine):
def drive(self):
print("Car drives")
class HybridCar(Car, ElectricEngine):
def eco_mode(self):
print("Eco mode enabled")
hybrid_car = HybridCar()
hybrid_car.start()
hybrid_car.charge()
hybrid_car.drive()
hybrid_car.eco_mode()The base class is the class being inherited from.
class Base:
def base_method(self):
print("Base method")The derived class inherits from the base class.
class Derived(Base):
def derived_method(self):
print("Derived method")
obj = Derived()
obj.base_method()
obj.derived_method()Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Dog barksThe super() function is used to call a method from the base class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
super().speak()
print("Dog barks")
dog = Dog()
dog.speak()When you initialize an instance of a derived class, the __init__ method of the base class is not called automatically. You must call it explicitly.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
dog = Dog("Buddy", "Golden Retriever")
print(dog.name)
print(dog.breed)class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"Vehicle Make: {self.make}, Model: {self.model}")
class Car(Vehicle):
def __init__(self, make, model, doors):
super().__init__(make, model)
self.doors = doors
def display_info(self):
super().display_info()
print(f"Number of doors: {self.doors}")
car = Car("Toyota", "Corolla", 4)
car.display_info()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 make_animal_speak(animal):
animal.speak()
dog = Dog()
cat = Cat()
make_animal_speak(dog)
make_animal_speak(cat)In this tutorial, we covered the concept of inheritance in Python, its benefits, and different types. We also explored method overriding, the super() function, and practical examples to understand inheritance better. With inheritance, you can create more organized, maintainable, and reusable code.
For more tutorials and in-depth explanations, visit codeswithpankaj.com!
This tutorial provides a comprehensive overview of Python inheritance, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!
| Back | FazBrowse Home | New Git URL |