preface

This article discusses a simple question: whether instantiating a child class instantiates a parent class.

This article is short and suitable for fragmentation time reading. This article will address this question in terms of bytecode and dump memory structures.

Albert Einstein: “If you cannot explain something simply, you do not really understand it.”

The difference between a=a+b and a+=b

Reload/rewrite, dynamic/static dispatch? (Revised)

Internal anonymous classes use external variables. Why add final

[short essay velocity -4] Whether the new subclass instantiates the parent class

Introduction to Multithreaded programming: Processes, threads, thread safety

Play the role

Little A: Just stepping into the path of Java programming…

MDove: A desperate Android developer…

To the chase

primers

Small A: MDove, I have been thinking about A question: will instantiating A child class instantiate its parent class?

MDove: That’s an interesting question. Let me ask you a question: can abstract classes be instantiated?

Small A: Although I am very bad, please don’t humiliate me! It is common sense that abstract classes cannot be instantiated.

MDove: Now that you know this, let me ask you again: subclasses inherit abstract classes. Now, if you instantiate the child class, do you think the parent class can be instantiated as an abstract class?

Little A: No…

MDove: Yes, that’s true. Then why do you still have questions?

Doubts point

Little A: Because when I was learning polymorphism, I found that the new subclass, the parent class constructor also executes, like this:

public static void main(String[] args) { SunClass sunClass1 = new SunClass(); } public class SunClass extends SuperClass { public String mSunName; public SunClass() { mSunName = "Sun Name"; System.out.println(mSunName); } } public class SuperClass { public String mSuperName; public SuperClass() { mSuperName = "Super Name"; System.out.println(mSuperName); }}Copy the code

Little A: If the constructor of the parent class is executed, won’t it also be instantiated?

Big mistake. Superclasses don’t instantiate

MDove: I don’t blame you for having such doubts. First, point out your mistake: the constructor being executed does not instantiate the class. Why is the constructor executed? That’s because the parent variable is being initialized.

MDove: Let’s look at the bytecode of this demo:

MDove: See the red line instructions? The constructor of a subclass calls the constructor of its parent class. Subclasses need to inherit variables and methods of type public/ Protect from their parent class. If there is inheritance, the constructor must be called to allocate memory.

Little A: In that case? If the parent is an empty implementation, don’t you need to call the constructor of the parent?

MDove: No, compile time will help us implicitly call.

MDove: I was wondering at first, why bother calling its constructor when the parent class doesn’t even assign a value to a variable? Yes, our parent class may not need to initialize a variable, but!! Its parent class is not necessarily unnecessary ah!! Remember, all of our classes implicitly inherit from Object. Tell me, how can we skip calling the parent class constructor and call the Object constructor directly? Obviously, it’s not easy to implement, so calling the superclass constructor step by step is a suitable solution!

Little A: So it’s true.

Look at the constructor from a memory allocation perspective

MDove: To avoid the paleness of words. Here I use some tools to give you a look at the memory allocation of objects.

The tool used here is AndroidStudio3.0, because I am an Android developer and AS3.0 does a great job of viewing memory, so I used this tool to expand this content.

MDove: I simply created a new project, a very simple operation:

findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
`   @Override
    public void onClick(View v) {
        newSunClass(); }});Copy the code

MDove: Let’s look at some memory allocation in the Android Profiler. Here I Dump the new subclass process:

MDove: See? There are only three main memory objects under our package. One is our MainActivity, which goes without saying. MainActivity$1 is our Listener. And SunClass is our subclass of new. Obviously, there are no superclasses! Let’s take a look at the memory allocation for SunClass:

MDove: mSuperName! And in the object of SunClass. Is that clear? The parent constructor is executed to assign the mSuperName variable, not to instantiate the parent.

Little A: What if I don’t assign A value to A variable in the constructor? Like this:

public class TestClass {
    public String name = "TestClass";

    public TestClass(a) {}}Copy the code

MDove: What you’re talking about is not assignment in constructors, it’s Java level assignment. This is clear when we look at bytecode:

MDove: See? When our Java files are compiled into class files. Our compiler writes bytecode to the constructor for the initialization of a member variable

If A parent class does not explicitly declare A no-argument constructor, the compiler implicitly adds A no-argument constructor. 2. If we do not explicitly call the parent constructor, the compiler will implicitly call it for us. Is it also through the form you said to do?

MDove: That’s right

Little A: It seems that we can’t take it for granted on the way of learning. We need to think more and practice more

The end

I am a fresh graduate, recently and friends maintain a public account, the content is that we in the transition from fresh graduate to the development of this way stepped on the pit, as well as our step by step learning records, if interested in friends can pay attention to it, together with fuel ~