Summary of Java subclass inheritance and final issues

Source: Internet
Author: User
Tags modifier

(1) If the subclass and the parent class are within the same package, the subclass can inherit all member variables and methods except private for the parent class, and the permissions will not change;

(2) If the subclass and the parent class are not within the same package, the subclass can inherit the member variables and methods of the public and protected permissions of the parent class, and cannot inherit the friendly and private;

(3) Further explanation of protected:

If there is a Class A, Class B is a subclass of Class A, Class C is a subclass of Class B, then Class C inherits the member variables and methods of Class A protected;

If the class C itself creates an object, you can use this object to access the inherited or own defined protected member variables and methods;

If you create an object in another class: for Class C's own defined member variables and methods, as long as the other class and Class D are in the same package, you can access the custom protected through the objects created by Class D;

For Class C inheriting from the parent class, the protected is traced back to the Ancestor class Class A, and if the other class and a are similar, the inherited protected can be accessed through the object.

(4) Hide and rewrite: The member variables of the subclass are the same as the members of the parent class, and the members that inherit from the parent class are hidden, and the methods that inherit from the parent class can be hidden by overriding the methods that inherit from them After hiding, you want to call a method that inherits from the parent class with the keyword super. Subclasses override the parent class method, and access permissions can be increased or unchanged, but must not be reduced.

(5) Final keyword: Final decorated class, class cannot be inherited; final modifier, method cannot be overridden; final modifier variable, variable is constant, must assign initial value.

Summary of Java subclass inheritance and final issues

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.