Other modifiers in Java

Source: Internet
Author: User
Tags modifiers

Abstract-abstracted

1, modified class-abstract class, if a class is abstract, then this class can not be instantiated, can only be inherited, so the abstract class is generally the parent class;

2, the method of modification-abstract method, if a method is abstract, then this method can not be implemented, the class containing abstract methods must be abstract class, but the abstract class does not necessarily contain abstract methods;

-If a non-abstract class inherits an abstract class, then this non-abstract subclass must implement all the abstract methods defined in the parent class, unless the subclass is abstract;

-If there is an abstract method in a class, then this class must be an abstract class. Conversely, if a class is an abstract class, there may be non-abstract methods in it.


-If you let a non-abstract class inherit an abstract class with an abstract method, an error will occur at compile time. Because when a non-abstract class inherits an abstract method, in which there is only one abstract method in a class, the class must be the principle of the abstract class. This class must be an abstract class, which is a non-abstract conflict with this class, so it is an error.


So the method of the subclass must override the abstract method of the parent class. method to work.
Only after the theory has been skillfully applied in the process of actual program design can the theory be fully mastered!


In order to implement polymorphism, the parent class must have a definition. The parent class is not implemented and is left to the subclass to implement. The parent class can now be defined as an abstract class. If you do not define an abstract parent class, the compilation error occurs.
Abstract and static cannot be put together, or an error will occur. (This is because static cannot be overwritten, and abstract must be overwritten in order to take effect.) )

Static -Statically

1, the modified property-static property, can be referenced by the class name, rather than using the object to reference, so the static property is categorized all, and not belong to a specific object, in fact, it is shared by all objects of the class;

-static properties are also called Class properties, and non-static properties are also called object properties.

-In general, we use the class name to refer to the static property without using the object to refer to it;

2, the Modification method-static method, can be called directly with the class name, do not need to use the object, commonly called class method;

-Static methods cannot refer to non-static members in a class;

Think: Why does the Main method have to be a static method?

3, static block-the code in the static block is executed immediately after the type information is loaded into the method area, and the function is to initialize the static property;

-Non-static members cannot be referenced in static blocks;

Problem-Singleton mode (at most only one time)-> A classic problem solved by static; Com. Firstjava.singleton

1, how to prevent users with the New keyword arbitrary generation of the object of the class---private construction method;

2, how to let the user get the object, give the user to provide a method, use this method to return to the user of the class object;

3, how to ensure the uniqueness of the object, using static properties;

Final -The ultimate

1, modifier class-The final class, the class cannot be inherited (the purpose is that the class can not be extended, that is, cannot be changed);

2, the modification method-the final method, the method can not be overridden by the quilt class;

3, modifier attribute-called constant, the name of the constant requires all letters are uppercase, its value cannot be changed, must have initialization;

4, modify local variables-final local variables can only be assigned once;

Inner class-Define another class in one class

1, the inner class is a member of the outer class, so all the keywords that can modify the members can be decorated with internal classes, or even static;

2. An inner class object can access all members of its outer class, including private;

Problem-Iterator pattern-collection class

Other modifiers in Java

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.