neurogift.blogg.se

Python oop inheritance example
Python oop inheritance example







python oop inheritance example

In this chapter, we saw the different ways we can implement it. Python OOP stands for Python Object-Oriented Programming. You were already aware of the concept of inheritance. Note that in super().getArea(self.length, self.breadth), the getArea() method of the parent class Area got called, and in super().getPerimeter(self.length, self.breadth), the getPerimeter() method of the class Perimeter got called.

python oop inheritance example

Similarly, we returned the perimeter of the rectangle using the class Perimeter. Another widely used library module, implemented almost all in pure python, and quite easy to understand.

#PYTHON OOP INHERITANCE EXAMPLE CODE#

The key to understanding Inheritance is that it provides code re-usability. We can represent this relationship in code with the help of inheritance. That means they’ve inherited the properties of class vehicles, i.e., all are used for transportation. This method then returned the area of the rectangle of length 7 and breadth 4. logging is one of most widely used, pure python, and to be honest, rather simple, standard library modules so it'd be worthwhile to study its design since the module's source code is not that long, and well documented. Here’s an example car, bus, bike all of these come under a broader category called Vehicle. Then the object called the method area() of the class Rectangle which called the method getArea(l, b) of its parent class Area assigning the values 7 and 4 to the parameters l and b respectively. So the length and breadth of rect became 7 and 4 respectively. When we created the object rect of the class Rectangle, its constructor got called and assigned the values 7 and 4 to the attributes length and breadth respectively for the object rect. The class Perimeter also has a method getPerimeter() which receives length and breadth and returns the perimeter. The class Area has a method getArea() which receives length and breadth and returns the area. In this example, the class Rectangle inherits two classes Area and Perimeter.









Python oop inheritance example