Python: Inheritance and polymorphism

Source: Internet
Author: User

Inheritance and polymorphism

In OOP programming, when we define a class, we can inherit from an existing class, the new class is called a subclass (subclass), and the inherited class is called the base class, the parent class, or the superclass (base classes, super Class).

For example, we have written a class named Animal , and there is a run() way to print it directly:

class Animal (object):     def Run (self):         Print ('Animal is running ... ')

When we need to write Dog and Cat class, we can inherit directly from the Animal class:

class Dog (Animal):     Pass class Cat (Animal):     Pass

For Dog speaking, Animal it's the parent class, and for Animal that, Dog it's a subclass. Catand Dog similar.

What are the benefits of inheritance? The biggest benefit is that the subclass obtains the full functionality of the parent class. Because the Animial method is implemented, run() Dog and Cat as its subclass, nothing is done, it automatically has the run() method:

Dog == Cat () cat.run ()

The results of the operation are as follows:

is is running ... 

Of course, you can also add some methods to the subclass, such as the Dog class:

class Dog (Animal):     def Run (self):         Print ('Dog is running ... ' )    def  Eat (self):        print('Eating meat ...  ')

The second benefit of inheritance requires a little improvement in our code. You see, either Dog or Cat , when they run () , they all display Animal is running ... , The logical approach is to display the Dog is running ... separately. And cat is running ... , so improvements to the Dog and Cat classes are as follows:

class Dog (Animal):     def Run (self):         Print ('Dog is running ... ' )class  Cat (Animal):    def  Run (self):        Print ('Cat is running ... ')

Run again and the results are as follows:

is is running ... 

Polymorphism : when the subclass and the parent class have the same run() method, we say that the child class overrides the parent class, and the subclass is run() run() always called when the code is running run() . In this way, we gain another benefit of inheritance: polymorphism.

To understand what polymorphism is, let's start with a little more explanation of the data type. When we define a class, we actually define a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:

# A is a list type # B is the animal type # c is the dog type

Judging whether a variable is a type can be isinstance() judged by:

>>> isinstance (A, list) True>>> isinstance (b, Animal) True>>>  Isinstance (c, Dog) True

It seems a , b and indeed corresponds to, c list Animal Dog these 3 kinds.

But wait, try it:

>>> isinstance (c, Animal) True

It c seems Dog to be more than just, c still Animal !

But think about it, it makes sense, because it's Dog Animal inherited, and when we create an Dog instance c , we think c the data type is Dog right, but it's c Animal also true, It Dog is Animal a kind of!

So, in an inheritance relationship, if the data type of an instance is a subclass, its data type can also be considered a parent class. However, the reverse is not possible:

>>> B = Animal ()>>> isinstance (b, Dog) False

DogCan be seen Animal , but Animal not seen Dog .

To understand the benefits of polymorphism, we also need to write a function that takes a Animal variable of one type:

def run_twice (animal):    animal.run ()    animal.run ()

When we pass Animal in an instance, we run_twice() print out:

 >>>is running ...

When we pass Dog in an instance, we run_twice() print out:

 >>>is running ...

When we pass Cat in an instance, we run_twice() print out:

 >>>is running ...

It doesn't seem to mean anything, but think about it now, if we define a Tortoise type again, it Animal derives from:

class Tortoise (Animal):     def Run (self):         Print ('tortoise is running slowly ... ')

When we call run_twice() , the incoming Tortoise instance:

 >>>is running slowly ...

You will find that a new subclass is not Animal necessary to run_twice() make any changes, in fact, any Animal function or method that relies on parameters can work without modification, because of polymorphism.

The advantage of polymorphism is that when we need to pass in Dog , Cat Tortoise ... , we just need to receive the Animal type, because,, Dog Cat Tortoise ... Are all Animal types, and then Animal you can do so by type. Because the Animal type has run() methods, any type passed in, as long as it is a Animal class or subclass, will automatically invoke the actual type of run() method, which is the meaning of polymorphism:

For a variable, we just need to know that it is a Animal type, without knowing exactly what its subtype is, you can safely invoke run() the method, and the method that is called is the function of the, run() or the Animal Dog Cat Tortoise object, Determined by the exact type of the object at run time, this is the true power of polymorphism: The caller just calls, regardless of the details, and when we add a Animal subclass, simply make sure that the run() method is written correctly, regardless of how the original code is called. This is the famous "opening and shutting" principle:

Open to extensions: Allow new Animal subclasses;

Closed for modification: You do not need to modify Animal run_twice() such functions as dependent types.

Inheritance can also be inherited from the first level, like from Grandpa to father, and then to the son of the relationship. And any class, in the end, can be traced back to the root class object, which looks like a backward tree. For example, the following inheritance tree:

                ┌───────────────┐                │    object     │                └───────────────┘                        │           ┌────────────┴────────────┐           │                         │           ▼                         ▼    ┌─────────────┐           ┌─────────────┐    │   Animal    │           │    Plant    │    └─────────────┘           └─────────────┘           │                         │     ┌─────┴──────┐            ┌─────┴──────┐     │            │            │            │     ▼            ▼            ▼            ▼┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐│   Dog   │  │   Cat   │  │  Tree   │  │ Flower  │└─────────┘  └─────────┘  └─────────┘  └─────────┘

Python: Inheritance and polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.