

Have to recreate this, since we can use it as-is. Method, which is defined on the Polygon parent class-we don’t Next, we’ve defined a new method area, which is onlyĪvailable on the Triangle class. This means that only the _init_ methodįrom the Triangle class is called, and not the one in the PolygonĬlass. _init_ method of the Polygon parent class, since we now need toĬheck that the sides that the shape is being given form a triangle,Īnd not some other shape. \(\theta_\) is an example of a bad value, so this would want to Say, forĮxample, we have a function to convert temperatures from degrees Celsius to We can also use this to define our own exceptions. We saw earlier that ValueError is a subclass of Exception, and that thisĬan be used to handle both specific and more general exceptions in a

The child class, subclass, or derived class. Superclass, or base class, while the new class inheriting from it is called The class that is being inherited from is called the parent class, A class that inheritsįrom a second class automatically gains all of the second’s attributes and Mechanism to avoid this in the form of inheritance. Thankfully, Python (and most other languages that have classes) give us a
#PYTHON OOP INHERITANCE CODE#
QuinticPlotter as well? This repetitive code would quickly start to build Of the code common to both of them? What if we wanted a QuarticPlotter and a However, what happens if we want to write two classes thatĭo similar but distinct things? For example, if we wanted to write aĬubicPlotter as well as our QuadraticPlotter, would we need to repeat all We have talked about using classes as a way to reduce repetition in the Be able to use inheritance to construct parent-child relationships between classesīe able to override methods on child classes, and refer back to the parent class’s implementations
