Java, Python object-oriented inheritance and its differences

Source: Internet
Author: User

Javajava inheriting basic styles
Class Demo extends object{    demo (int a) {this        ();    }    Demo () {        super ();    }}

Java inherits the object class by default and is bound to invoke the super () method in the constructor to instantiate the parent class. Note: this () and super () cannot appear in the same constructor at the same time, but super () must appear in one of the constructors!

Java Instantiation Process
    1. Run the static code portion of the parent class
    2. Rerun the static code portion of the subclass
    3. Executing the parent class member code
    4. Executes the parent class constructor--The parent class completes the instantiation
    5. To execute a member code block of a subclass
    6. Constructors for executing subclasses--instantiation of subclasses complete
Some details of the Java instantiation process

Pythonpython inheriting basic styles
Class Demo (object):    def __init__ (self,a):        super (). __init__ ()        SELF.A = a

The Python instantiation process is the same as Java, which inherits object by default and requires that the parent class be instantiated.

Python instantiation Process
    1. Executing static code for the parent class
    2. Executing a subclass of static code
    3. Execute Subclass __new__ () method
    4. Calling the parent class __new__ () method in the subclass __new__ () method
    5. Call the parent class __init__ () method (parent instantiation complete)
    6. Call Subclass __init__ () method (subclass instantiation complete)
python instantiation Process

Contrast inheritance

When a Java object calls a method in the parent class, the method called by the method takes precedence over the method of the subclass, and the member variable that is called takes precedence over the variable of the parent class.

When a Python object calls a method in the parent class, the methods and variables that are called take precedence over the methods and variables of the child class.

Polymorphic

Python Natural polymorphism

Java: Because of the problem of variable properties, you must demote and convert the objects to use the methods and properties of the child classes.

Animal a = new Cat ();//a does not use the Cat class function, all properties and methods are Animal the parent class of cat C = (cat) A; To use

In the example above, when the variable type is the parent class or interface of the instance type, there are differences in compile and run:

    1. Member variables: Both compile and run refer to animal = left!
    2. Method: Compile Reference animal = left, parent class has only compile pass, run priority reference Cat is reference = right. (The method of a subclass is still used in the case of duplicate names, but if you call a method that the parent class does not have, it cannot be compiled.) )
    3. Static part: Because there is no need for the object, there is no this, are reference animal = left!

Java, Python object-oriented inheritance and its differences

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.