Summary of usage methods for common modifiers in Java
- 2020-05-27 05:47:55
1: public protected default private
Modifies a class, modifies a method, modifies a property, modifies a block of code.
Top-level classes can only be decorated with public, and top-level classes cannot be decorated with private and protected.
External classes can be modified by public or not written by default, private and protected cannot be used.
Inner classes can be static and can be decorated with protected and private.
Normally a method can be modified with four access modifiers, and a constructor can be modified with four access modifiers.
Abstract methods in an abstract class cannot be modified by private, but can be modified by three other methods.
Methods in the interface can only be modified by public (also public by default).
Properties can be modified by four modifiers, and properties in an interface can only be modified by public (also public by default).
Block of code:
A code block cannot be modified by any modifier (because a property or a method inside a code block has its own modifier).
Construction blocks are used: non-static properties can be initialized, construction blocks take precedence over constructor execution, and can be placed in constructors.
Static code blocks can be used to initialize static properties, class loads, and not in constructors.
Modifier class: final class, which cannot be inherited. The final class usually completes the class of 1 definite standard function, for example, Java library mathematics class Math and so on.
Modifier: methods modified with final cannot be inherited or overridden (for example, wait() in Object), but can be overridden.
Constants: define local constants with final, and global constants with static final (or final static).
Modifier class: you can modify inner classes
Modify properties: static can modify any property.
Modifier: cannot modify abstract methods, cannot modify constructors.
Modified code block: the modified code block is called a static code block
Modifier class (abstract class) : an abstract class cannot be instantiated. Abstract classes are meant to be inherited.
Modifier (abstract method) : a method that can be modified is called an abstract method. It has only the declaration of the method and no implementation of the method, ending with a semicolon.
Cannot modify: cannot modify properties, private methods, constructors, static methods, final methods.
What keywords can't abstract coexist with?
final: if the method is abstract, it needs to be overridden. The final modifier cannot be overridden.
private: if a function is private, subclasses cannot access it directly and cannot override it
static: class method that makes no sense to call abstract methods directly.