Python 3 Deep Dive Part 4 Oop [NEW]

def area(self): return self.width * self.height

print(my_car.make) # Output: Toyota my_car.honk() # Output: Honk honk! Inheritance is a fundamental concept in OOP that allows one class to inherit the attributes and methods of another class. The class that is being inherited from is called the parent or superclass, and the class that is doing the inheriting is called the child or subclass. python 3 deep dive part 4 oop

class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self

class ElectricCar(Car): def __init__(self, make, model, year, battery_size): super().__init__(make, model, year) self.battery_size = battery_size class Rectangle: def __init__(self, width, height): self

Here's an example of inheritance in Python 3:

The ElectricCar class also has its own attribute battery_size and method charge . Polymorphism is the ability of an object to take on multiple forms. In Python 3, polymorphism can be achieved through method overriding or method overloading.

The honk method is an example of a method that can be called on an object of the Car class. To create an object from a class, you use the class name followed by parentheses, like this: