java-Inheritance and coverage "excerpt"

Source: Internet
Author: User

Inherited

(1) Java does not support multiple inheritance, which means that subclasses can have at most one parent class

(2) subclasses inherit member variables and member methods that are not private in their parent class, as their own member variables and methods

(3) The member variables defined in the subclass and the member variables defined in the parent class are not inherited by the member variables in the parent class

(4) A member method defined in a subclass, and the name of the member method, the return type, and the number and type of arguments are exactly the same as a member method of the parent class, the member methods of the parent class cannot be inherited.

1. Constructors:
When a subclass inherits a parent class, the constructor of the parent class needs to be called when the subclass is constructed, there are three cases
(1), the parent class has no constructor or a parameterless constructor, if the subclass does not have a constructor or parameterless constructor, the subclass constructor does not need to explicitly call the parent class constructor, the system will automatically call the subclass constructor before calling the parent class constructor
(2), the parent class has only the parameter constructor, the subclass must display the constructor of the calling parent class in the constructor method, or the compilation error
(3), the parent class has both a parameterless constructor and a parameter constructor, and the subclass can not call the constructor of the parent class in the constructor, and the parameterless constructor of the parent class is used
The above three conclusions have been verified by code
2. Method overrides:
(1) The subclass overrides the parent class's method, must have the same parameter return type, otherwise the compilation cannot pass through
(2) Subclasses override methods of the parent class, after jdk1.5, the parameter return class can be a subclass of the parent class method return class
(3) Subclasses override the parent class method, you can modify the method scope modifier, but you can only enlarge the scope of the method, and cannot change public to private
(4) Subclass method has access to the protected scope member of the parent class and cannot access the default scope member
(5) A static method of a subclass cannot hide a parent class instance method with the same name
(6) Java as with C + +, inherited methods are polymorphic
The above 6 conclusions have been verified by code
3. Member Coverage:
(1) When a subclass overrides a member variable of a parent class, the parent class method uses the member variable of the parent class, and the subclass method uses the member variable of the child class
This sounds easy to understand, but the actual use of a lot of people are easy to confuse: especially in polymorphic, call an inherited method, the method access is an overridden member M, then the method is to access the members of the parent class or the members of the sub-class m? The conclusion is that if the method of the parent class is actually called, the member m of the parent class is used, and if the method of the subclass is actually called, the member m of the subclass is used, remembering that each class using a member is equivalent to adding a this pointer in front.

Stonecao-csdn

java-Inheritance and coverage "excerpt"

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.