Recently, I have some experience in assigning subclass objects to parent class objects, and I would like to share it with you. But my level is limited, so please correct me and criticize me.
Closer to home, here are a few small examples, please take a look.
Test one
Parent class:
Subclass:
Result: this is the parent class print() method - the object at this time is Subclass@126b249
At this time the object is Subclass@126b249
illustrate:
Supclass sup=new Subclass();
Although the declared object is a parent class object, the actual memory space belongs to the subclass object.
The method public void print() inherited from the parent class is called, and the output is the name resolution of the subclass object.
Conclusion: The object declared at compile time is a parent class object, but at runtime it is a subclass object. If the subclass does not override the method of the parent class, the object at this time calls the method inherited from the parent class.
Test 2
Parent class:
Subclass:
Result: this is subclass print() method - at this time the object is Subclass@126b249
At this time the object is Subclass@126b249
illustrate:
Based on the previous example, I rewrote the print() method of the parent class. At this time, the print() method of the subclass was called.
Conclusion: Based on the conclusion of the previous example, I came to a conclusion: at this time, the object is indeed a subclass object when running. If the subclass does not write the method of the parent class,
Then the object at this time calls the method inherited from the parent class; otherwise, the object at this time calls the subclass method.
Question: Can we draw the following conclusion from the above test: assign the subclass object to the parent class object (i.e. Supclass sup=new Subclass()),
What we get is a subclass object, that is, sup is a subclass object??????
Test three
Parent class:
Subclass:
Result: Attributes at this time: parent class attributes
Note: Based on the first test, I added an attribute className to the parent class and overridden this attribute in the subclass.
But when I output the properties of the object at this time, they are the properties of the parent class.
Conclusion: When assigning subclass objects to parent class objects, the methods and properties are very different from our orthodox inheritance relationship.
question:
Is the object at this time a subclass object or a parent class object?
Start speculating:
There are a few points I need to make before speculating:
1. When we create a new subclass object, the constructor of the parent class object is also executed at the same time, that is, some necessary information of the parent class and the subclass object occupy the same memory space.
When we override the method, we can use the super object to refer to the parent class.
2. Objects in Java are not completely object-oriented, that is, the attributes and methods of an object are not encapsulated into the object at the same time.
But the object has its own attributes, but the method refers to the method in the class. It can be said that it encapsulates the references of the attributes and methods in the class into the object.
So the method called by the object is not its own method, but the method in the class. As for why Java does this, I don't know.
3. When the object is loaded into the memory, the class is loaded into the memory first, and then the class should remain in the memory. As for when the class disappears from the memory, I don’t know.
I think Java must have its own recycling mechanism, just like recycling objects.
4. Compiling and running are completely different things. The main things you do during compilation are to declare the type of the object, assign attributes, check for syntax errors, etc.
What the runtime does is load objects into memory (usually using new, reflection is also commonly used), run code execution functions, etc.
5. If you, the reader, and I do not resonate with each other on these points, or if we do not have the same understanding on these points, you will think that I am talking nonsense.
Maybe you will think that my expert score is too low, and therefore my credibility is low. But what I want to say is that there is no order of learning, and those who master first come first.
Haha, I'm ready to turn my expert points into negative values, no more nonsense, let's continue.
Speculation:
1. When we compile Supclass sup=new Subclass(), the sup object is declared as the Supclass class, so the attributes of the sup object are the values of the attributes of the parent class object.
3. Continuing from step 2, if we override the method of the parent class, since the memory space of the sup object is the memory space of the subclass object, the subclass method public void print() has been loaded into the memory.
So what we call is the method public void print() of the subclass; if you need to call the overridden method of the parent class, you need to use super.
This passage can explain test 2.
Summarize:
The following are purely personal opinions:
Assign the subclass object to the parent class object, and the resulting object is an object like this:
It is compiled as a parent class object, but runs as a subclass object. The specific characteristics are as follows.
1. Declared as a parent class object 2. Has parent class attributes 3. Occupies the memory space of the subclass 4. When the subclass method overrides the parent class method, the object calls the subclass method at this time; otherwise, inheritance is automatically called Methods of the parent class.
5. I think this object is neither a parent class object nor a subclass object. When we use its methods,
I treat it as a subclass object; if I use its properties, I treat it as a parent class object.
It is an object that occupies the attributes of the parent class and uses the methods of the subclass. As for what kind of object it is, I think it depends on the declaration. It should be regarded as a parent class object, but has subclass methods.
Think about it:
Based on test 3, how do we extract the attributes of the subclass? ? ? ? ?