Java object-oriented programming--seventh chapter inheritance

Source: Internet
Author: User

7.1 Origins of the Inheritance

Inheritance (inheritance), that is, in object-oriented programming, you can create a new class by extending (extends) an existing class and inheriting the behavior of the class's properties.

An existing class is called a parent class (also known as a base class, a superclass), and a new class is called a subclass (also known as a derived class).

The advantages of inheritance: The reusability of the Code, the properties and methods of the parent class can be used for subclasses, subclasses can extend the properties and methods of the parent class, and the design of the application becomes simpler.

7.2 Principles of use of inheritance

    • Observing the classes to be used, identifying common and unique properties and behaviors between them, migrating these common data into the parent class, facilitates the reuse of methods and properties in subclasses.
    • For different properties and behaviors, you can redefine them in subclasses.
    • In Java, a class has only one parent class. A class cannot inherit two classes at a time.
    • The root class of all classes is Object

When a subclass inherits a parent class, it produces a parent class object before the child class object is produced, and then extends the properties and methods that are unique to the subclass. The subclass can therefore inherit all the properties and methods of the parent class. Therefore inheritance is called also extension.

7.3 Rewriting

Overrides occur between a parent class and a subclass, with the same method name as the parent class, the same argument list, and the same return type. When the method is called, the subclass override method is called first. If the subclass does not override the method of the parent class, then the parent class's method is called.

Before the method, you can add annotation @override to force detect whether the current method overrides the parent class method. If there is no override to the parent class method, the compilation error.

Finalize () does some cleanup work before garbage collection objects.

7.4 Super Represents the parent class object, and Super () represents the parent class's construction method, which produces the parent class object. The subclass inherits from the parent class, and the virtual opportunity automatically adds super () if the constructor of the parent class is not called by Super () in the subclass construction method to produce the parent class object. If no parameterless construction method is provided in the parent class, the subclass must display the call through Super (argument). Otherwise, the subclass cannot use super () to make a call to the parent class without a parameter construction method, thus compiling the error. Super () must be the first line of code for a subclass construction method

7.5 Final

The keyword final indicates that the variable cannot be modified, that the method cannot be overridden, and that the class cannot be inherited.

When a final type modifies a variable, if the base type is decorated, the value of the underlying type cannot be changed, and if the reference type is decorated, it means that the variable cannot point to another object, but the object's property value can be changed.

7.6 The constructor method cannot be inherited by the quilt class, but when the subclass object is created, the parent class's constructor is called to produce the parent class object. Because the child class object contains the full parent class object, all properties and methods in the parent class object are inheritable (including private properties and private methods). Only private properties and private methods cannot be accessed directly by the quilt class.

Java object-oriented programming--seventh chapter inheritance

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.