The compiler does not simply create default objects for each reference, which can reduce unnecessary burden. If you want to initialize these references, you can do so at the following position: 1. Define the object; 2. In the constructor of the class; 3. before using the object (inert initialization); 4. Use instance initialization.
Classes are always inherited when they are created. If it is not explicitly specified to inherit from other classes, objects are inherited by default. The inherited classes in other packages can only access the public members and methods of the parent class. Generally, the parent class members are set to private and the methods are set to public. in the inheritance class, you can call the method of the parent class. If the method name is the same, the super keyword is added before the method name. From the external perspective, the export class is like a new class with the same interface as the base class. There may be additional methods and fields, but inheritance is not just a copy of the base class interface. When an export class object is created, the object contains a sub-object of the base class. Java automatically adds a call to the base class constructor In the constructor of the export class. If no default base class constructor exists, you must explicitly call the base class constructor with parameters.
Java does not provide direct support, and is the medium way to inherit and combine. Place a member object in a class (similar to a combination) to be constructed, but all methods (similar to inheritance) of the member object are exposed in the new class ). You can have more control when using the proxy. You can choose to only provide a subset of the member object method.
public class DirectionControls {
void left( int i){}
void right( int i){}
void forward( int i){}
void back( int i){}
}
public class Car {
DirectionControls controls= new DirectionControls();
void left( int i){
controls.left(i);
}
void right( int i){
controls.right(i);
}
public static void main(String[] s){
Car car= new Car();
car.left(1);
}
}
Sometimes you must explicitly write a method for cleaning, because you do not know when the GC will be executed, and the cleaning process must be placed in the finally clause. In the inheritance structure, the cleanup action should be in the opposite order of object generation. The cleanup action first calls the subclass and then the base class.
Defining a method with the same name in an export class does not block the versions in the base class. This is effective both in the export class and in the base class. When you want to override a method of the base class, add the @ override flag to prevent unexpected overloading without reload. The override will not block the base class version. Note:
Override (rewrite)
1. The method name, parameter, and return value are the same. (The return value type of the subclass method is the child type of the parent class method.) 2. The subclass method cannot reduce the access permission of the parent class method. 3. The subclass method cannot throw more exceptions than the parent method (but the subclass method can not throw an exception ). 4. exists between the parent class and the Child class. 5. methods defined as final (including private) cannot be overwritten.Overload)1. There must be at least one different parameter type, number, and order. 2. The method names with different return values cannot be reloaded. 3. It exists in the parent class, Child class, and similar class.
- Combination and inheritance
Both of them allow sub-objects to be placed in the new class, and the combination is explicit to inherit implicit. A combination is usually used when you want to use the functions of an existing class in a new class instead of an interface. Is the has-a relationship. Inherit from existing classes (general classes) and develop a special version (special version ). Is-a link. In Oop, it is most likely that data and methods are directly packaged into a class and then this object is used. You can also use a combination of existing classes to develop new classes, but inheritance is not commonly used. The best way to determine whether to use combination or inheritance is to determine whether to perform upward transformation from the new class to the base class.
The role is to hide some things in this world as much as possible, but still allow members of the export class to access them (with package access permissions ). Although you can create a protected domain, it is better to use the private domain to control the access permission of the export class through the protected method. (Reserve the right to change the underlying implementation for yourself)
Inheritance ensures that all methods in the base class are equally valid in the export class. All messages that can be sent to the base class can also be sent to the export class. Conversion of exported class references to base class references is called upward transformation. The export class is a superset of the base class. During the upward transformation, the method may be lost in the class interface, but it is safe.
Final refers to "This cannot be changed", probably because: design or efficiency. The difference between the two may be misused. Final usage: data, methods, and classes. Final data: it is usually used for 1. A compilation that never changes is often used for 2. It is initialized at runtime, but you do not want it to be changed. A static final domain occupies only a segment of storage space that cannot be changed. All domain names are in uppercase and separated. For basic types, final makes the value constant, and for object reference, final makes the reference constant. Once initialization points to an object, it cannot point to another object, but the value of the object itself can be changed. Blank final: The domain declared as final but not given an initial value. It allows the final domain in the class to be different based on the object, but remains unchanged. For example, initialize parameters in the constructor. Final parameter: You can declare the parameter as final in the parameter list. In the method, you cannot change the object to which the parameter reference points. This feature mainly transmits data to anonymous internal classes. Final method: cause: Lock the method to prevent modification of the inheritance class, and ensure that the behavior of the method remains unchanged and will not be overwritten. The private method is implicitly set to final. Final class: When a class is defined as final, this class cannot be inherited. You never need to make any changes to the design of the class, or you do not want it to have a subclass out of security considerations. The domain of the final class is not affected by the final of the class. The methods in the final class are all final by default, regardless of whether there is any final keyword.
- Initialization and loading of Classes
Java does not have dependency on the loading sequence between static variables. Everything in Java is an object, and the compilation code of each class is stored in an independent file. This file is loaded only when program code is used. The constructor is also a static method, and the class is loaded when any static member accesses it. Loading Sequence: 0. The storage space allocated to the object is initialized to a binary value of 0 or null before doing anything. 1. The static members and static initialization blocks of the parent class are executed in sequence in the code.
2. Subclass static members and static initialization blocks are executed in sequence in the code.
3. The parent class instance members and instance initialization blocks are executed in sequence in the code.
4. parent class Constructor
5. Subclass instance members and instance initialization blocks are executed in sequence in the code.
6. Subclass Construction Method