Hi, this is the fourth video of the series, objects and classes in Python. In this video, we'll be discussing inheritance in classes. You already know the concept of inheritance. For example, like I inherited my skin color from my father, and from my mother. And my hair colors are are inherited from my mother. It's more like, like my mother's hair color.
So, in simplest of meaning, inheritance means getting it from someone else. Here, inheritance means getting the attributes and methods from another class. And we call that parent class. The class that inherits attributes and characteristics and methods from another class is called the child class. For example, when we define a class like this bird, whose color is red, and then we Create a inheritance method that is self executable because it has in it in it. So, another method is who is this and then the swim method.
This is the these are the three methods within the class, but then we create another class this is a bang penguin class. Now, this inherits all the characteristic from the bird class, because we have given or passed a parameter bird within the parentheses of this class. What it actually does, it gets all the content of that parent class into this child class. That means, bird is the parent class and penguin is the child class because penguin is inheriting characteristics from the bird class. Now, here, there are two init method. One is in the parent class and the second one is in the child's last So, which one will actually execute when when we are going to create an object from the penguin class.
Then, if we are looking to execute both the init method we can use super function alongside here just before init method to what it does it gets all the content of the init method of the parent class into this child class. And this is what I have written here inherited parent class separate self executing methods super function calls parent class content benefits class content to this child class. Okay, now, there is another question if there are two similar method in both parent class and child class, then the child class method will override the parent last method, if we are using or instantiating an object from the child class, for example. Now, I have created an object from the penguin class. When I run this, what is going to do is going to run this that bird is ready because I've used a super function in it just before and it method within the init method and then it is going to run penguin is ready.
And then if we call Who is this function then it is going to run Who is this function and it will it will print penguin and then Peggy dot swim. Now swim function is not in this child class but it has inherited This function or method from method from child from parent class of bird, so, swim faster and Peggy dot run run is there already there in the child met child class. So it will run it will print run faster. Now in case if there are two methods that are similar or they are exactly the same for example, who is this And who is this? which method will run in that case, the child class method will override the parent class method but if it is not given the parent class method will be running now, let's create a method let's create an object from the parent class.
Jerry equals to bird you see I use this bird class and then Jerry dot Who is this Jerry dot Who is this now it should print penguin No, it should print bird because I've used parent class and this is what it does bird Okay. Now, this is again an example of overriding here we know that we have we have created another class some other bird and we are getting all the characteristics of the bird class into it and then we are overriding it overriding the class variable from color red to color white, initially we used color red. However, in this class, we used color white and then when we instantiate the some other bird class into an object, what it print, it prints color is white So, What it has actually happened? It all right? This color instance to this color instance. Okay, in this video we have a small exercise you have to create an animal class and a bird class that inherits characteristic from animal class and it has its own attributes like the bird class has its own attributes.
So this is a very simple exercise for this lecture. I hope you understand that our discussion was very simple, and I tried to keep it as clear as possible with examples. May you have any queries feel free to contact me? I'd be more than happy to answer you. Thank you.