Java Notes Collation (v), inheritance in Java

Source: Internet
Author: User

1. Concept of inheritance

Inheritance is a relationship between classes and classes, and is a relationship of "is a"

2. Benefits of Inheritance

Subclasses have all the properties and methods of the parent class to implement code reuse

4. Rewriting of Methods (overridding)

If the subclass is not satisfied with the method inherited from the parent class, it is possible to override methods that inherit from the parent class, and when called, the subclass's methods are called first

The return value type, the method name and the parameter type, and the number of arguments are the same as the methods in the parent class, which is called the override of the method

5. Initialization Order of inheritance

1) Initialize the parent class first, and then initialize the child class

2) Initialize the properties in the initialization object before performing the initialization in the constructor method

6.final keywords

Use the final keyword as an identity with a "final" meaning

The final keyword can decorate classes, methods, properties, and variables

Final decorated class, the class is not allowed to be inherited

Final decoration method, the method is not allowed to be overwritten (overridden)

The final decorated property, where the properties of the class are not implicitly initialized (the class's initialization property must have a value) or assigned in the constructor method, only one

Final modified variable, the value of the variable can only be assigned once, which becomes a constant



7.super keywords

Used inside an object, you can represent the parent class object

If the constructor method of the calling parent class is not displayed in the constructor of the subclass, the system defaults to calling the parent class without a parameter construction method

If the constructor method of the child class is displayed in the constructor of the subclass, then super must be in the first row of the child class's construction method

If there are no parameter constructs in the parent class, the constructor method in the subclass implicitly calls the constructor of the parent class, and if there is no parameterless constructor in the parent class, and the subclass does not explicitly call the constructor of the parent class, an error is compiled



Java Notes Collation (v), inheritance in Java

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.