Usage and comparison of Java four rights modifiers (public, default, protected, private) __java

Source: Internet
Author: User
Tags modifier modifiers visibility
permission size for four rights modifiers (public, protected, default, private)

Note: Default, does not have this keyword, it thinks that defaults, that is, nothing added!
Generally, the default visibility is package visibility (package visibility).

This class under the same package (subclasses and unrelated classes) under different packages (subclasses) under different packages (unrelated classes)
Private Y
Default Y Y
Protected Y Y Y
Public Y Y Y Y
summary of common modifiers used by classes and their composition in object-oriented
Modifiers:
    permission modifiers: private, default, protected, public
    status modifier: Static, Final
    abstract modifier: Abstract

1. Class:
    Permission modifiers: default,public
    status Modifiers: final
    abstract modifier: Abstract
    The most used is: public

2. Member variables:
    permission modifiers: private, Default,protected,public
    State modifier: The
    most used static,final is: Private

3. Construct method:
    permission modifier: Private,default, Protected,public
    uses the most is: public

4. Member Method:
    permission modifier: private,default,protected,public
    Status modifiers: static,final
    Abstract Modifiers: Abstract The
    most used is: public

5. Other than the combination of rules:
    member variables: public static final
    Member method: Public
        static public
        abstract public
        final
related note points:Abstract cannot co-exist with the following keywords
Private
(When the class and member method is decorated, if there is an abstract method in the abstract class, it should be inherited by the quilt class, and all the abstract methods of the parent class are overridden in the subclass, and the class cannot be inherited, the member method cannot be accessed) final
(For the reason above, when the final modifies the class and member methods, the class cannot be inherited and the Member method cannot be overridden) static
(If you decorate with static, you only pass the "class name." You can call the abstract method directly, obviously, it doesn't make any sense. When all member methods in a class are static (all static), the constructor must be private (plus private).
(Example: In Java.lang, the Math class calls its constants and static methods –math.pi and Math.Abs (a) directly without creating objects) Summary

In the object-oriented, three characteristics (encapsulation, inheritance, polymorphism) of the understanding is particularly important, but at the same time understand the four kinds of permissions modified relationship, for who can call who, whether there is access rights and other issues, to solve more handy! Of course, this article is the first time I used Markdown Editor to write in Csdn, it feels good!


This article address: http://blog.csdn.net/menglanyingfei/article/details/55210673
This article originates from: http://blog.csdn.net/menglanyingfei?viewmode=contents
I hope that you can comment and guide my article to communicate and learn from each other, but also welcome you to my blog to see the article you have helped, but also welcome to reprint, but I would like to indicate the origin of the hyperlink in the clear location! Thank you!

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.