[Java Basics] 13. abstract methods cannot be static or native or synchroniz. nativesynchroniz
I have seen such an article on the internet because the abstract method cannot be static, native, or synchroniz. The meaning of these keywords and the conflict with abstract keywords are mentioned. The general content is as follows:
1. abstract is abstract. It means that a method is declared but not implemented. Its implementation should be implemented in the subclass that declares the class.
2. static is static. It is a method or attribute that belongs to a class rather than an object. We know that a class is actually an object, it is an object generated after the class file is loaded to the virtual machine. Generally, it is a Singleton, that is, the entire virtual machine has only one such class Object (of course, if a new class loader is used, a new class object will be generated ).
3. synchronized is a synchronization lock relative to a thread.
4. native local method. This method is similar to the abstract method. It only has a method Declaration and has no method implementation. However, unlike the abstract method, it transfers the specific implementation to the function library of the local system without passing through the Virtual Machine. It can be said that it is a mechanism for Java to communicate with other languages.
Then the author analyzes the cause of the conflict one by one:
First, abstract and static. In fact, it is easy to identify their roles and attributes. abstract is not implemented, but static must be implemented, because abstract classes cannot produce objects, however, static is a class, and the class is already an existing object. The two keywords have a key conflict between them.
Synchronized is synchronous, But synchronization requires specific operations to be synchronized. If abstract only has a method declaration, synchronizing something will become a problem, of course, after the quilt class inherits the abstract method, you can add synchronization.
Native, this item is in conflict with abstract. They are both method declarations, but one method implementation is handed over to the subclass, and the other is handed over to the local operating system. If it appears at the same time, it is equivalent to transferring the implementation to the subclass and the implementation to the local operating system. Who will implement the specific method?
The author has no objection to the cause of conflicts between synchronized and native keywords. The key lies in the reason of static conflicts.
In the section marked in red in the third paragraph, the author says that "classes are actually an object". It is okay if you know a little about JVM class loading mechanisms. In the subsequent analysis, the author said that "abstract classes cannot generate objects, but static is a class, and classes are already an existing object", which has no objection. The key lies in the following: "The two keywords have conflicts on this key point." The author obfuscated the concepts of class objects and instance objects.
No matter whether it is an abstract Class or not, as long as this Class is used, the virtual machine will always create a Class object for this Class in the method area to describe the runtime structure of this Class. Let's write a piece of code for a simple test, as shown below:
[Java]View plain copy
- Public class MonitorVehicleTracker {
- Public static void main (String [] args ){
- System. out. print (Door. class. getClassLoader ());
- }
- }
- Abstract class Door {
- Public static void set (){
- System. out. print ("123 ");
- }
- }
The running result is:
Sun. misc. Launcher $ AppClassLoader @ 15253d5
We can see that the abstract Class Door is indeed loaded (because we can access its Class Object), and it is loaded by the application Class loader.
That is to say, "abstract Class cannot produce objects" means that abstract Class has its corresponding Class object in the method area, but this Class object cannot generate instance objects. Therefore, it is incorrect for the author to analyze the contradiction between abstract and static.
So what is the reason why abstract and static cannot be used at the same time?
We know that the abstract method has the significance of quilt coverage and polymorphism. Static methods can only have one copy in the memory, so sub-classes can inherit but cannot be rewritten. When a field or static method is used to access an object by reference, the reference type determines the field actually accessed, rather than the class actually represented by the current reference. The static method of subclass completely does not reflect polymorphism. Therefore, if a method is abstract and static, It subverts the abstract class and the meaning of the abstract method itself!
The reference type determines the static method of the class to be accessed. The test code is as follows:
public class Test { public static void main(String[] args) { Door door = new AlarmDoor(); door.get(); }}abstract class Door { public static void get() { System.out.print("Door"); } abstract void set();}class AlarmDoor extends Door { public static void get() { System.out.print("AlarmDoor"); } @Override public void set() { // TODO Auto-generated method stub }}
Output result: Door