Inheritance in Python

preview_player
Показать описание
#python #oop #inheritance
In this lecture, we'll explore inheritance in Python, which is a key concept in Object-Oriented Programming.
We'll start by understanding what inheritance is and its importance.
Next, we'll create a simple parent class and then define a child class that inherits from it.
We'll go through how to override methods and add additional methods in the child class.
We'll also see how the child class can use inherited attributes and methods.
Then, we'll briefly touch on multiple inheritance and use the functions isinstance() and issubclass() to check instances and class relationships.

Inheritance allows a new class, known as the child class, to inherit attributes and methods from an existing class, which is referred to as the parent class.
This helps us reuse code and makes our programs more organized.
In Python, the parent class is also called the base or super class, while the child class is known as the derived or subclass.

Let's get started by creating a simple parent class.
In the file, let's define a class called Animal.
We'll include an __init__ method to initialize some attributes for this class.
It initializes two attributes, name and species, using the values passed to the method when an instance of the class is created.
We can also add a method called 'make_sound' that prints a message including the name.
Now, let's create an instance of this class to see how it works.
We do this by calling the Animal class and passing values for name and species.
Here, we created an instance called my_animal and set the name to 'Buddy' and the species to 'Dog'.
This shows how the instance 'my_animal' has the attributes and behavior defined in the Animal class.
Let's run the script.
We should see this output, confirming that our parent class is working as expected.

Now that we have our parent class, let's create a child class that will inherit from it.
But before that, I'll add a few horizontal lines using comments starting with a hash followed by repeating dashes.
This helps visually separate different parts of our code, making it easier to identify and navigate the sections as our script grows longer.
Now, in the same file, let's define a new class called Dog that inherits from Animal.
Note that we used parentheses to specify the parent class that the new child class will inherit from.
Next, let's add a constructor method that takes name and breed as parameters.
Inside the constructor, we call the parent class's constructor.
This allows us to set the name attribute via the parent class and fix the species attribute to 'Dog'.
Note that the super() function is used to have access to the parent class and call its __init__ method to set up inherited attributes.
After that, we'll add a new attribute called breed, which is specific to the Dog class.
It must be initialized using the breed input parameter.
So, we created a new class called Dog that inherits from Animal.
Next, let's define an instance of the Dog class.
We name this instance 'my_dog' and use the Dog class constructor to initialize it with the name 'Buddy' and the breed 'Golden Retriever'.
The next line calls the 'make_sound' method on the 'my_dog' instance.
Since the Dog class inherits from the Animal class, it uses its 'make_sound' method.
The next line prints the name and breed attributes of the 'my_dog' instance.
The name attribute is inherited from the Animal class, while the breed attribute was added specifically in the Dog class.
This demonstrates how the Dog class can use both inherited attributes from its parent class and new attributes defined within itself.
Let's run the script again.
As expected, the Dog class inherited the 'make_sound' method from the Animal class, and also utilized its own breed attribute.

Sometimes, we want to change the behavior of a method in the child class. This is called method overriding.
Let's override the 'make_sound' method in the Dog class to provide a more specific message.
We redefined the 'make_sound' method in the Dog class to print a message indicating the dog is barking.
Now, let's call the 'make_sound' method of our 'my_dog' instance.
Since we have overridden this method in the Dog class, it will use the new version defined in Dog, rather than the one from the Animal class.
Let's run the script again with the updated class definition.
This time, we see that the Dog class uses its own version of 'make_sound' instead of the one in its parent class.

In the Dog class, we already added an attribute breed. Let's now add a new method that is unique to the Dog class.
I'll add a method called fetch that takes an argument item and prints a message saying that the dog is fetching it.
Let's try using this new method and passing 'ball' as input.
Then, if we run the script,
We'll see this output...
Рекомендации по теме
welcome to shbcf.ru