Python Basics

The essential guide on Python Inheritance: Beginner python tutorials 14 | Better4Code

Python Inheritance: Python is an object-oriented programming language that supports inheritance, which is the ability to create new classes based on existing classes. Python Inheritance allows you to reuse code and avoid duplication by defining a base class that contains common attributes and methods and then creating new classes that inherit from it. In this article, we’ll explore Python inheritance and provide some examples to illustrate its usage.

The essential guide on Python Inheritance: Beginner python tutorials 14 | Better4Code

Defining a Base Class

To define a base class in Python, you use the keyword “class” followed by the name of the class. Here is an example:

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Generic animal sound")

This creates a base class called Animal with two attributes (name and species) and a method called make_sound.

 

Python Inheriting from a Base Class
To inherit from a base class in Python, you define a new class and include the base class as a parameter in the definition. Here is an example:

class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name, "Dog")
self.breed = breed

def make_sound(self):
print("Woof!")

This creates a new class called Dog that inherits from the Animal class. The Dog class has an additional attribute called breed, and a new implementation of the make_sound method that prints “Woof!”.

Accessing the Base Class
To access the attributes and methods of the base class in a derived class, you use the super() function. Here is an example:

my_dog = Dog("Rex", "Labrador")
print(my_dog.name) # Output: Rex
print(my_dog.species) # Output: Dog
my_dog.make_sound() # Output: Woof!

This creates a new Dog object called my_dog and accesses its name, species, and make_sound attributes. The super() function is used to call the init method of the base class, passing in the name and “Dog” as arguments.

Multiple Python Inheritance
Python also supports multiple inheritances, which is the ability to inherit from multiple base classes. Here is an example:

class Pet:
def __init__(self, owner):
self.owner = owner

class Cat(Animal, Pet):
def __init__(self, name, owner):
Animal.__init__(self, name, "Cat")
Pet.__init__(self, owner)

def make_sound(self):
print("Meow!")

This creates a new class called Cat that inherits from both the Animal and Pet classes. The Cat class has an additional attribute called owner, and a new implementation of the make_sound method that prints “Meow!”.

 

Conclusion
In this article, we explored Python inheritance and provided examples to illustrate its usage. We learned how to define a base class, inherit from it, access the base class, and use multiple inheritance to inherit from multiple base classes. By understanding these concepts, you can take full advantage of the object-oriented programming features of Python and create complex, reusable code.

gp

Are you looking to learn a programming language but feeling overwhelmed by the complexity? Our programming language guide provides an easy-to-understand, step-by-step approach to mastering programming.

Leave a Reply

Your email address will not be published. Required fields are marked *