Java Basics !!, Java Basics
Question 1: Why cannot abstract keywords coexist with private, static, and final keywords?
① Private indicates private. When it modifies a method, the subclass cannot inherit the parent class's private method. However, the abstract modifier must inherit and implement the quilt class, conflict between the two. Static means static. static means shared. static means static methods are called when the method is modified. static means can be called without creating objects, when there is an inheritance relationship, abstract modifies the abstract and cannot be instantiated. abstract methods must be implemented by subclasses and conflict with static. Final is the final meaning, that is, it cannot be inherited or overwritten. abstract modification methods must be rewritten by subclasses, so they are also conflicting. Abstract can only be used with public and protected.
② Static:
A) static variables. Variables modified by static are similar to global variables (this concept is not available in Java ). When this class is loaded by the VM for the first time, the memory space is allocated to the variable. When this class creates an instance, it does not generate a copy of the static variable. But multiple instances of this class share this variable. All objects of this class can operate on this bucket. If final is used for modification, it is another matter. After the creation is complete, Initialization is required. You can initialize the definition directly. If you need to initialize your static variable through calculation, you can declare a static block. The Static block is only executed once when the class is loaded, and when the class is loaded for the first time.
Note: initialization of static variables takes precedence over any other non-static variables, regardless of the order.
When inheritance is involved, the static variables of the parent class will be initialized first, then the static variables of the Child class, and so on. You can directly use the class name. variable name and share it with all the instantiated objects of the class. It can be used by all methods in the class (static and non-static ). A variable value is modified for an object in this class, and the corresponding values of all other objects in this class change accordingly. Initialization during definition or through static code block Initialization
B) static methods. static methods are called class methods. You can call this method directly through the class instead of creating an instance of this class.
Note: You can directly use "class name. method name. Only other Static methods can be called. Only static member variables can be used. This and super cannot be referenced in any form
Purpose: static methods often provide some practical tools for other classes in the application. A large number of static methods in Java class libraries are defined for this purpose. Arrays and Collections
C) Static class. Generally, a common class cannot be declared as static. Only one internal class can be declared. In this case, the static internal class can be directly used as a common class without the need to instance an external class.
Supplement: static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables. The static modified member variables and member methods are independent of any object in the class. It is shared by all instances of the class and does not depend on a specific instance of the class. As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.
Static member variables and member Methods Modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of static variables is not generated, instead, all instances of the class share the same static variable.
The static variable can be modified in private, indicating that the variable can be in the static code block of the class, or other static member methods of the class (you can also use -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much easier to understand. The effect of adding other access key words before static is also similar.
Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is: Class Name. static Method Name (parameter list ...); class Name. static variable name. A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed.
Static variables: class member variables can be classified by static or not. One is static modified variables, such as static variables or class variables; the other is a variable that is not modified by static, called an instance variable. The difference between the two is: for static variables with only one copy in the memory (memory saving), the JVM only allocates memory for static variables once, and completes the memory allocation of static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ). If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).
Static Method: The static method can be called directly through the class name, and can be called by any instance. Therefore, the this and super keywords cannot be used in the static method, you cannot directly access the instance variables and instance methods of the class (that is, the static member variables and member methods are not included ).
③ Final
Features: A) use final modified variables to represent constants. Values can only be assigned once and cannot be modified. Final-modified basic type variable: The value cannot be modified. Final-modified reference type variable (object): the object address cannot be modified, and members in the object can be modified. Object Reference defined as final can only point to one unique object. It cannot point to another object, but the internal value of an object can be changed. The variable modified by final is a constant and must be assigned a value before use. You can assign values when defining them, or assign values in the constructor. (Assign a value before the constructor ends .)
B) The final modification method cannot be overwritten by the quilt class;
C) Classes modified with final cannot be inherited, and there is no subclass. The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. However, the member variables in the final class can be defined as final or non-final form. When designing a class, if the class does not need a subclass and the implementation details of the class cannot be changed, it is designed as a final class.
D) final cannot be used to modify the constructor.
④ Static and final: static and final are used to modify member variables and member methods. For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name. For methods, it indicates that they cannot be overwritten and can be directly accessed by class names.
Solution 2: differences between abstract classes and interfaces
A) abstract class: the class containing abstract modifiers is an abstract class, and the abstract class cannot create instance objects. Classes that contain abstract methods must be defined as abstract class. Methods in abstract class do not need to be abstract. Abstract methods defined in the abstract class must be implemented in the Concrete subclass. Therefore, there cannot be abstract constructor or abstract static methods. If the subclass does not implement all abstract methods in the abstract parent class, the subclass must also be defined as abstract.
B) interface: it can be said that it is a special case of an abstract class. All methods in the interface must be abstract. The method definition in the interface defaults to public abstract, and the member variable type in the interface defaults to public static final.
C) Differences:
- Abstract classes can have constructor methods, and interfaces cannot have constructor methods.
- Abstract classes can contain common member variables. The interface does not contain common member variables.
- Abstract classes can contain non-Abstract Common methods. All methods in interfaces must be abstract and cannot have
- Non-Abstract Common method.
- The access type of abstract methods in the abstract class can be public or protected, but the abstract methods in the interface can only be public, and the default type is public abstract.
- Abstract classes can contain static methods. Interfaces cannot contain static methods.
- The abstract class and interface can contain static member variables. The access type of static member variables in the abstract class can be any, but the variables defined in the interface can only be of the public static final type, the default value is public staticfinal.
- A class can implement multiple interfaces, but can inherit only one abstract class.