Analysis of Java decoration appended public default protected private static final abstract

Source: Internet
Author: User
Tags modifiers

Analysis of Java decoration appended public default protected private static final abstract

the function of a modifier: used to define a class, method, or variable, usually at the front end of a statementTwo classification (broadly divided into two categories)1. Access modifiers, including: public default protected Private2. Non-access modifiers, including: Static final abstractthree access modifiers in--java, you can use access controls to protect access to classes, variables, methods, and construction methods. Java supports 4 different kinds of access rights. 1.public: Common, visible to all classes 2.default: default, visible within the same package, without using any modifiers. 3.protected: Protected, classes within the same package and all subclasses can be 4.private: private, visible within the same class

5. Access control and inheritance-note the rules inherited by the following methods:
    • Methods declared as public in the parent class must also be public in the subclass.

    • A method declared as protected in a parent class is either declared as protected in a subclass, or declared as public, and cannot be declared as private.

    • The method declared as private in the parent class cannot be inherited.

 PackageBokeyuan;/*Same Package Type*/ Public classModifier_test1 { PublicString name= "Hero";// Public    protected intnum=188;//protected           Booleanresult=true;//Default Defaults    PrivateString country= "China";//Private         Public voidGetName () {System.out.println (name);    }; protected voidGetnum () {System.out.println (num);    };  Public voidGetResult () {System.out.println (result);    }; Private voidGetcountry () {System.out.println (country);        };  Public Static voidMain (string[] args) {modifier_test1 Superdemo=NewModifier_test1 ();  Superdemo.getname (); //Output HeroSuperdemo.getnum ();//Output 188Superdemo.getresult ();//Output TrueSuperdemo.getcountry ();//Export China            }}
 PackageBokeyuan;/*different classes of the same package*/ Public classModifier_test2 { Public Static voidMain (string[] args) {modifier_test1 Superdemo=NewModifier_test1 ();  Superdemo.getname (); //Output HeroSuperdemo.getnum ();//Output h188Superdemo.getresult ();//Output TrueSuperdemo.getcountry ();//Error    }}

 PackageBokeyuan1;/*different Packages*/ImportBokeyuan. Modifier_test1; Public classModifier_test3 { Public Static voidMain (string[] args) {modifier_test1 Superdemo=NewModifier_test1 ();  Superdemo.getname (); //Output HeroSuperdemo.getnum ();//ErrorSuperdemo.getresult ();//Output TrueSuperdemo.getcountry ();//Error    }}

four non-access modifiers1.static modifiers for creating class methods and class variables

A. When the static keyword is used to declare a static variable that is independent of an object, no matter how many objects a class instantiates, its static variable has only one copy. Static variables are also known as class variables. Local variables cannot be declared as static variables

B. When the static keyword is used to declare a static method that is independent of an object. Static methods cannot use non-static variables of a class. Static methods get the data from the argument list and then calculate the data

2.final modifiers, which are used to decorate classes, methods, and variables.

A.final-Modified classes can not be inherited;

B. The method of modification can inherit the quilt class, but cannot modify the quilt class;

C. The modified variable is a constant and is not modifiable.

3.abstract modifiers, used to create abstract classes and abstract methods.

A. The abstract class is called an abstraction, and is used to instantiate an object, declaring that the abstract class is only intended to be expanded in the future

B. The method of abstraction is called an abstract method, which is a method without any implementation, and the specific implementation of the method is provided by the subclass

C. If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes may not contain abstract methods

4.synchronized and volatile modifiers, primarily for threading programming

Analysis of Java decoration appended public default protected private static final abstract

Related Article

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.