Inheritance

Inheritance is one of the most powerful aspects of classes. First off, classes allow you to modify a program without really making changes to it. To elaborate, by making a subclass through inheritance, you can change the behavior of the program by simply adding new components to it rather than rewriting the existing components.

As we've seen, an instance of a class inherits the attributes of that class. However, classes can also inherit attributes from other classes. Hence, a generic superclass (also known as a parent) can be specialized through subclasses (also called children), since the children classes inherit their general properties from the parent class. The subclasses can override the logic in a superclass, allowing you to change the behavior of subclasses without changing the superclass at all.

Let's look at a simple example, such as following screenshot:

 Class inheritance

First we make a class on line 43. This will be the superclass. This class has two methods, just like the preceding screenshot. Next, we make a subclass on line 44.

As you can see, the child class overwrites the display method but, since there is no explicit reference or redefining of the set_data() method, that particular method is inherited as is. When an instance of the Parent class is created, all of its actions will be taken from the methods defined in Parent. When a Child instance is created, it will use the inherited, generic set_data() method from Parent but the display() method will be the new one created in Child.

Instances of the superclass and subclass are created on lines 45 and 46. Both instances use the same set_data() method from Parent; x uses it because it's an instance of Parent while y uses it because Child inherits set_data() from Parent. However, when the display() method is called, x uses the definition from Parent (line 49) but y uses the definition from Child (line 50), where display is overridden.

Because changes to program logic can be made through subclasses, the use of classes generally supports code reuse and extension better than traditional functions do. Functions have to be rewritten to change how they work, whereas classes can just be sub-classed to redefine methods.

It should be obvious now that a class instance can access all attributes from the classes they are part of; that is, a specific instance can access its own class methods, as well as any parent classes it is inherited from. Thus, you only have to overwrite or create specific attributes as necessary, relying on the inherited attributes to be available to the instance.

On a final note, you can use multiple inheritance (adding more than one superclass within the parentheses) if you need a class that belongs to different groups. In theory, this is good because it should cut down on extra work.

For example, a person could be a chef, a musician, a store owner, and a programmer; the person could inherit the properties from all of those roles. In reality, though, it can be a real pain to manage the multiple inheritance sets, as methods are inherited, overridden, and otherwise obfuscated. In short, multiple inheritance is something that beginning programmers should avoid until they understand how Python deals with it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.226.121