Public,private,protected in Java, and the default difference

Source: Internet
Author: User

Scope current similar Bun class other

Public√√√√

Protected√√√x

Default √√xx

Private√xxx

Defaults to default when members of a class do not write access adornments. The default is equivalent to exposing (public) to other classes in the same package, and for other classes that are not in the same package are equivalent to private (private). A protected (protected) subclass is equivalent to exposing a class that is not a parent-child relationship in the same package as private.

The meaning of the access modifier in Java, public, private, PROTECTE, default, is explained:
Public (interface Access): The most restrictive modifier in the Java language, commonly referred to as "common". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
Private (You cannot access): The narrowest modifier for access restrictions in the Java language is generally referred to as "private". Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
Protected (inherited access): An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
Default (Package access): That is, without any access modifiers, often referred to as the "Default access mode". In this mode, access is allowed only in the same package.

Public,private,protected in Java, and the default difference

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.