Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the Java class inherit Object by default

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how the Java class inherits Object by default". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the Java class inherits Object by default.

Preface

Anyone who has studied Java knows that Object is the parent of all classes. But do you have such a question, I didn't write extends Object, how does it inherit Object by default?

So today let's take a look at how virtual machine-dependent programming languages like Java inherit Object by default, and how do Java compilers and JVM do it?

Inherited from Object authentication

First, let's verify whether Object is the parent of all classes, and randomly create a new Java class, as shown below:

As can be seen from the above code, there are many methods that can be called after new MyClass () management, and there is only one main method in the MyClass class that we define, so where do these methods come from is obviously declared in Object, so the parent class of the MyClass class is Object, so the public or protected resources of the Object class can be used in MyClass.

In addition, when class An inherits the MyClass class, it can also be adjusted to the method within Object by typing, which is the inheritance transmission, for example, Object is the "father" of MyClass, MyClass is the "father" of class A, and Object is the "grandfather" of class A, inheriting Object indirectly.

Therefore, Object is a superclass and the parent of all classes.

Speculate on possible causes

To understand how the Java class inherits Object by default? You don't really need to know the implementation details of JVM. You only need to understand the basic principles of this kind of virtual machine program. There are generally two ways to deal with default inheritance issues for languages that run on virtual machines (such as Java, C #, and so on).

Compiler processing

When compiling source code, when a class does not explicitly indicate an inherited parent class, the compiler assigns it a default parent class (usually Object), and when it is handed over to the virtual machine to process the class, because the class already has a default parent class, VM will still deal with the class in the same way as other classes. In this case, from a compiled binary point of view, all classes have a parent class (which can be verified later).

JVM processing

The compiler still compiles according to the actual code and does no extra processing, that is, if a class does not explicitly inherit from another class, the compiled code still has no parent class. Then when the binaries are run by the virtual machine, when a class that does not have a parent class is encountered, the class is automatically treated as a subclass of the Object class (usually the default parent class of this type of language is Object).

Verify the conclusion

As you can see from the above two cases, the first case is an article done on the compiler, that is, when there is no parent class, the compiler automatically assigns a parent class to it at compile time. The second case is to work on the virtual machine, that is, the default parent class is added by the virtual machine.

So which situation does Java belong to? In fact, this answer is easy to come up with. You just need to find a random decompiler and decompile the .class file to know how the compiler compiles.

Take the above code as an example, in the first case, even if MyClass does not have a parent class, but because the compiler has automatically added an Object parent class for MyClass, the MyClass class in the source code obtained after decompilation will inherit the Object class. If this is not the case, then it is the second case.

So what is the actual situation? Now let's decompile MyClass.class and see what happens.

Jd-gui retweet:

Decompilation using JDK's native tool (javap)

Execute under the CMD command line:

Javap MyClass > MyClass.txt

You can see that there is no extends Object in the actual decompiled file, and exclusion is used, so this is the second case.

The conclusion drawn from this is the second case, but is this really the case? Why is it still said on the Internet that the decompiled one has the word extends Object?

What's wrong with the JDK version?

Guess it's the JDK version, so switch the JDK version to 7, decompile it with jd-gui and javap, and get the same result as decompiled with JDK8, without extends Object.

Continue to change the version, last night in the dormitory ready to Oracle official website to download JDK 6, but can not come, the first thing to do after arriving at the company this morning is to download, very smooth, after installation, switch the JDK version to JDK 6.

Still execute javap MyClass > MyClass.txt in the CMD window, and the resulting TXT file reads as follows:

What? Unexpectedly, there is still no extends Object,jd-gui decompiled. That is, the MyClass class decompiled with javap before JDK 6 explicitly inherits Object,JDK 7, and the decompiled jd-gui does not exist no matter how the JDK version is. We refer to the tools that come with java.

Summary

So it means that JDK 6 is processed by the compiler before, and after JDK 7 is processed by the virtual machine.

But think carefully that when we IDE in the editor, we can list the methods under the Object class. Before the compiler and jvm turn, the editor already knows that the parent class of the MyClass class is the Object class, because the editor has done some intelligent processing for us.

At this point, I believe you have a deeper understanding of "how the Java class inherits Object by default". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report