Java Access control variables

Source: Internet
Author: User

1. Private: In the same class

2. Default: Classes in the same package

3. Protection: In a class subclass in the same package (inheritance)

4. Public: No scope

When you create subclasses and override methods, you must consider the access control of the original method:

As a general rule, the overriding method is that the access control of the new method cannot be more restrictive than the original method, but it can be more loose.

Access control involving methods:

1. methods that are declared public in a superclass must also be common in subclasses;

2. A method declared as protection (protect) in a superclass can be protected (protect) or public in a subclass ( public), but cannot be private.

3. Methods that do not have access control in the superclass can be more restrictive in their access control in subclasses, or they can be relaxed.

Java Access control variables

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.