8/15/2023 0 Comments Python inheritanceNow, when we call the eat() method using the object of the Dog subclass, the method of the Dog class is called. In the above example, the same method eat() is present in both the Dog class and the Animal class. # call the eat() method on the labrador object # attributes and method of the parent class This concept is known as method overriding in Python. In this case, the method in the subclass overrides the method in the superclass. However, what if the same method is present in both the superclass and subclass? In the previous example, we see the object of the subclass can access the method of the superclass. This repeats recursively, if the base class is itself derived from other classes. If an attribute is not found in the class itself, the search continues to the base class. Here, we can see that even though we did not define methods like inputSides() or dispSides() for class Triangle separately, we were able to use them. # Calculating and printing the area of the triangle # Prompting the user to enter the sides of the triangle # Creating an instance of the Triangle class # Using Heron's formula to calculate the area of the triangle # calling the _init_ method of the Polygon class # Initializing the number of sides of the triangle to 3 by # method to display the length of each side of the polygon Now let's see the complete working code of the example above including creating an object, class Polygon: However, the Triangle class has a new method findArea() to find and print the area of the triangle. Print('The area of the triangle is %0.2f' %area) We don't need to define them again (code reusability). This makes all the attributes of Polygon class available to the Triangle class. So, we can create a class called Triangle which inherits from Polygon. The dispSides() method displays these side lengthsĪ triangle is a polygon with 3 sides.The inputSides() method takes in the magnitude of each side.This class has data attributes to store the number of sides n and magnitude of each side as a list called sides. Say, we have a class called Polygon defined as follows, class Polygon: Let's take a look at another example of inheritance in Python,Ī polygon is a closed figure with 3 or more sides. Here, Car can inherit from Vehicle, Apple can inherit from Fruit, and so on. That is, we use inheritance only if there exists an is-a relationship between two classes. In Python, inheritance is an is-a relationship. This is possible because the subclass inherits all attributes and methods of the superclass.Īlso, we have accessed the name attribute inside the method of the Dog class using self. Here, we are using labrador (object of Dog) to access name and eat() of the Animal class. Notice the statements, labrador.name = "Rohu" In the above example, we have derived a subclass Dog from a superclass Animal. # access name attribute of superclass using self # attribute and method of the parent class Here, we are inheriting the sub_class class from the super_class class.Įxample 1: Python Inheritance class Animal: Here's the syntax of the inheritance in Python, # define a superclass The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class). Inheritance allows us to create a new class from an existing class. Like any other OOP languages, Python also supports the concept of class inheritance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |