java--Object-oriented

Source: Internet
Author: User
Tags control characters instance method modifier modifiers

1. Defining classes

class class Name {    0 to multiple constructor definitions ...    0 to multiple member variables    ... 0 to multiple methods ...}

1.1 Defining constructors

[modifier] Constructor Name (parameter list    ) {//  constructor name must be the same as    class name //  composed of 0 to more executable statements }

2. Modifiers

2.1 Modifiers applicable range

External classes/Interfaces Member properties Method Constructors Initialize block member Inner class Local members
Public
Protected
Package access O O
Private
Abstract
Final
Static
Strictfp
Synchronized
Native
Transient
Volatile
Default
Same color cannot appear at the same time (or there is no meaning at the same time (gray))

2.2 Modifier Usage Details

Classes (Class) ([12] [~34]) Method ([1234] [+] [~67])
Public Can be used by all classes (requires the same class name as the file name) Public 4. Can be accessed in the global scope
Default (Empty) can only be accessed by other classes of the same package Protected 3. Can be accessed by sub-classes of different packages
Abstract Abstract class, cannot create instance, only as parent class Default (Empty) 2. Can be accessed by other classes of the same package
Final will not be able to be used as the parent class Private 1. Can only be accessed within the current class
Member variables ([1234] [+] [~]) [1] Static Without this adornment, you can only invoke it through an instance.
Public 4. Can be accessed in the global scope Abstract Classes with abstract methods can only be defined as abstract classes
Protected 3. Can be accessed by sub-classes of different packages Final Subclasses are not allowed to overwrite the final method of the parent class
Default (Empty) 2. Can be accessed by other classes of the same package Constructor (no return value declaration)
Private 1. Can only be accessed within the current class (common) Public 4. Global scope accessible (constant)
[1] Static Through its adornments, it can be called through a class or instance Protected 3. Can be accessed by sub-classes of different packages
Final Re-assigning a value to the final variable is not allowed Default (Empty) 2. Can be accessed by other classes of the same package
Initialize block {} (executed before execution of the constructor) Private 1. Can only be accessed within the current class
Static (Cannot initialize an instance variable) executes during the class initialization phase, rather than when the object is created. Private modifies all constructors of this class, and subclasses cannot invoke the class constructor, and thus cannot inherit. This class will not be able to create an instance directly, but you can return the instance and control it by creating a (public static class method) (P174)
Default (Empty)
The This reference cannot be used in static decorated methods, so static decorated methods cannot access ordinary members that do not use the static adornment
Inner class ([1234] [+] [~67])

The final decorated member variable must explicitly specify an initial value.

(1. Class variable: The initial value must be specified in the static initialization module or when declaring the class variable;

2. Instance variable: The initial value must be specified in a non-static initialization module, declaring the instance variable, or the constructor

Public 4. Can be accessed in the global scope
Protected 3. Can be accessed by subclasses of different packages of external classes
Default (Empty) 2. Can be accessed by other classes of the same package as the external class
Private 1. Can only be accessed within the current external class
Static Non-static inner classes cannot have static members The final modifier local variable can be specified at the time of declaration, or it can be assigned an initial value in subsequent code, but only once.
Abstract Abstract class, cannot create instance, only as parent class
Final will not be able to be used as the parent class Final modified formal parameter, the form is called when the method is invoked by the system to complete the initialization according to the parameters passed in, so it cannot be assigned a value
The internal class defined in the interface uses the public static modifier by default
The local inner class (defined in the method) cannot use the access control modifier The final modification of reference variables only guarantees that the address of the variable reference is unchanged
All local variables cannot use the static modifier Abstract modified classes can only be inherited abstract cannot and static/private modify a method at the same time
Anonymous inner class: New Implementation interface () | Parent class Constructor (argument) {}
Anonymous inner classes are suitable for creating classes that only need to be used once The abstract modified method is defined without {}, and only methods that do not implement the abstract adornment must be provided by the subclass to provide the implementation (override)
J8 before the anonymous/local inner class can access only the final local variable J8 automatically uses the final decoration for the local variable that it accesses
Enum class enum (same as class, interface status)
Inherited One or more interfaces can be implemented
The default inherits Java.lang.Enum instead of object, so the enumeration class cannot display inheritance from other parent classes
Enum class implements the Java.lang.Serializable and java.lang.Comparable two interfaces
Use Enume-defined, non-abstract enum classes by default with final
Constructors You can only use private (can omit) access control characters
An instance of an enumeration class must appear listed (', ' separated) in the first row of the enumeration class, or the enumeration class will never produce an instance. When you list these instances, the system automatically adds a public static final decoration without the programmer's display additions.
Values () method Easy traversal of all enumerated values

3. Interfaces (defining common behavior specifications for multiple classes)

[public | empty] Interface Interface name extends parent interface 1, parent interface 2, ... (unlike class, the interface supports multiple inheritance)
A Java source file can have at most one public interface, and the source file must have the same primary file name as the interface
The member in the interface is public access, the modifier can be omitted (the system is automatically specified), the specified word can only use public
Member variables can only be static constants. Always use public static final to decorate (can be omitted, system is used automatically)
Method Abstract instance method Always use public abstract to modify (can be omitted), there is no method body
Class method (J8) You need to use the static modifier, you cannot use the default adornment
Default method (J8) You need to use the default modifier, you cannot use the static modifier
Inner class Internal interface, enumeration. Always use public static to decorate (can be omitted)
[Modifier] class class name [extends parent class] implements interface 1, interface 2, ... (Implementation of the interface)
Implementing interface Methods can only be modified with public because the child class must have greater or equal access permissions when overriding the parent class method

java--Object-oriented

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.