Python Tutorial for Beginners 17 - Subclasses , Superclasses and Inheritance

hello guys in this blog I am going to show you what is inheritance and what are subclasses and super classes in Python so we all know that Python is an object-oriented programming language and inheritance is one of the most important concepts in object-oriented programming language now what is inheritance inheritance allows us to define a class in terms of another class which makes it easier to create and maintain our application so for example you have created a class which is generic in nature and you want to you reuse it again and again in different situations you can use inheritance to you know show or inherit some of the traits or all the traits from the class to your class which you want to make so you don't need to write a class from the scratch you can inherit from the superclass the class from which we are inheriting is called a super class and the class which is inheriting is called a subclass so let me give you an example for example I create a class which I call this is a parent parent ok and this class has two values or two variables value one is equal to let's say it's a string so I say this is value one and in the same way I will define value to this should be value right so I will define value to variable which is the second member variable of this class and this is value to okay now for example I want to reuse the variables which are their member variables which are there in the parent class and I want to create a second class and I want to reuse these the member variables in other class how we can do it so I will define a second class for example class and I will name it as child and this class wants to reuse these member variables from the parent class so what I can do is I can inherit from the parent class in the child class and to inherit the parent class in the child class we just use these two brackets and inside these two brackets we just pass the name of the class from which we want to inherit so in this case we want to inherit from this parent class so we can just pass this parent class name and in the brackets right now we can use this values from the instance of our child class so for example I don't want to do anything here I can just write pass and pass if you don't know what is passed pass is just a placeholder so you know it doesn't do anything but you suppose you don't want to implement your class and you want to leave your class as it is then pass will help you to avoid any errors so let's see what happens when we don't use pass here and I will save my program and run it and it says unexpected indent session then should not be here run it once again so it shows an error and what is the error because we haven't given a pass here it expect or Pythian expect that we define some you know definitions of member variables of member functions here so what we can use is we can use pass in order to avoid any errors now whenever we want to make a child class instance for example child like this is equal to and we want to make the instance of this child class we can make it like this so we just can make the instance child you can just kill underscore to differentiate this instance from this class but let's first create an instance of the parent class and then we will create the instance of child parent & parent is equal to parent so we have created the instance of parent class and from this instance we can call parent dot value one which is this one and we just want to print this parent dot value one so you can just print and inside our print you can just pass this parent dot print one here and let's say we will define it this child class later so I will just run this once again ad prints this is value one okay in a similar manner you can call second value from parent and this you already know right now suppose child instance want to use value one and value two I create the instance of child and because I am inheriting from my parent class this value 1 or and value 2 which are member for member variables of this parent class is available to use from the instance of child class also because by inheritance we are allowing the superclass which is this parent class from which we are inheriting so the class from which we are inheriting is called super class and the class which is inheriting from super class is called subclass so from this instance we can call this member when values or variables from the parent class so I can just once again copy this print value 1 but this time I will use the instance of child class and you can see here I haven't implemented anything in the child class except the inheritance right so I will just run the program once again and it should it prints this double x so for example I delete this to illustrate this example clearly l1 say in one this ad prints this is value one and this is value to which is printed by the instance of child class right that means we can use the member variables or member when use of parent class when we be inherit from parent class to a child class right now you can inherit from multiple classes so for example you have parent class one you have parent class two and you want to inherit from both the classes you can inherit from parent one like this comma parent - like this okay so you can even inherit from two classes or multiple classes at the same time you just need to separate those parent class by this comma okay so just remember this so in this way you can use inheritance in Python and I hope you have enjoyed this blog 😊

Post a Comment

أحدث أقدم

Recent in Technology News