Java assigns a subclass object to the parent class Object

Source: Internet
Author: User

Test 1
Parent class:

Public class supclass
{
Public void print ()
{
System. Out. println ("this is the print () method of the parent class" + "-- the object" + this. tostring ());
}
}

Subclass:
Public class subclass extends supclass
{
Public static void main (string [] ARGs)
{
Supclass sup = new subclass ();
Sup. Print ();
System. Out. println ("Current object" + sup. tostring ());
}
}

Result: This is the print () method of the parent class. The object is subclass @ 126b249.
The object is subclass @ 126b249.

Note:
Supclass sup = new subclass ();
Although the declared object is a parent class object, the actual memory space is a subclass object.
The method that inherits the parent class public void print () is called, and the output is the resolution of the subclass object name.

Conclusion: The object declared during compilation is a parent class object, but it is a subclass object during runtime. If the subclass does not override the parent class method, the object calls the method that inherits the parent class.

Test 2

Parent class:
Public class supclass
{
Public void print ()
{
System. Out. println ("this is the print () method of the parent class" + "-- the object" + this. tostring ());
}
}

Subclass:

Public class subclass extends supclass
{
Public void print ()
{
System. Out. println ("this is subclass print () method" + "-- the object" + this. tostring ());
}
Public static void main (string [] ARGs)
{
Supclass sup = new subclass ();
Sup. Print ();
System. Out. println ("Current object" + sup. tostring ());
}
}

Result: This is the print () method of the subclass. The object is subclass @ 126b249.
The object is subclass @ 126b249.
Note:
Based on the previous example, I have rewritten the print () method of the parent class. At this time, the print () method of the subclass is called.

Conclusion: Based on the conclusion in the previous example, I have come to the conclusion that the object does not write the parent class method if the subclass does not exist during the object runtime,
The object calls the method that inherits the parent class; otherwise, the object calls the subclass method.

Question: Can we conclude from the above test that the subclass object is assigned to the parent class Object (supclass sup = new subclass ()),
We get the subclass object, that is, sup is the subclass object ??????

Test 3

Parent class:
Public class supclass
{
Protected string classname = "parent class attribute ";
Public void print ()
{
System. Out. println ("this is the print () method of the parent class" + "-- the object" + this. tostring ());
}
}

Subclass:
Public class subclass extends supclass
{
Protected string classname = "subclass attribute ";
Public void print ()
{
System. Out. println ("this is subclass print () method" + "-- the object" + this. tostring ());
}
Public static void main (string [] ARGs)
{
Supclass sup = new subclass ();
System. Out. println ("when the attribute is:" + sup. classname );
}
}

Result: At this time, the property of the parent class is

Note: On the basis of the first test, I added an attribute classname to the parent class and overwritten this attribute in the subclass.
However, when I output the property of the object at this time, it is the property of the parent class.
Conclusion: Assign the subclass object to the parent class object. The methods and attributes have different inheritance relationships with our Orthodox.
Problem:
In this case, is the object a subclass object or a parent class object?

Start to speculate:

I have to declare the following points before speculation:

1. When we create a new subclass object, the constructor of the parent class object is also executed, that is, some necessary information of the parent class and the subclass object occupy a memory space,
When we rewrite the method, we can use the super object to represent the parent class.

2. Objects in Java are not completely object-oriented, that is, they do not encapsulate the attributes and methods of an object in the object at the same time.
Instead, the object has its own properties, but the method references the methods in the class. It can be said that it encapsulates the reference of the attributes and methods in the class into the object.
Therefore, the method called by the object is not its own method, but a method in the class. I don't know why Java is doing this.

3. when an object is loaded into the memory, the class is first loaded into the memory, and the class should be left in the memory. I don't know when the class will disappear from the memory.
I think Java must have its own collection mechanism, just like recycling objects.

4. Compilation and running are completely different. during compilation, the main task is to declare the object type, allocate attributes, and check syntax errors.
During the runtime, the object is loaded into the memory (usually new, reflection is also commonly used), and the code execution function is run.
  
5. If you and I do not resonate with each other at these points, or we do not have the same understanding at these points, you will think that I am talking nonsense.
Maybe you will think that my experts are too low and I feel that my credibility is low, but what I want to say is that learning is not sequential, and reaching first.
Well, I'm ready to turn my expert score into a negative value. Let's continue.

Speculation:
1. When supclass sup = new subclass () is compiled, the sup object is declared as a supclass class. Therefore, the property of the sup object is the property value of the parent class object,
It is determined during compilation. (Statement 4, which can be explained ). this section explains Test 3, that is, why the object's attribute is the attribute of the parent class object.
  
2. When we run supclass sup = new subclass (), the memory space of the sup object is the memory space of the subclass object (Declaration 4, which can be explained in this section ),
Note that the memory space of the sup object consists of the necessary information of the parent class and the information of the subclass object (Declaration 1, which can be explained in this section ).
When we do not override the parent class method, because the memory space already has some necessary information about the parent class, the method inheriting the parent class public void print () can certainly be called.
This section explains Test 1.

3. continue 2. If we rewrite the parent class method, because the memory space of the sup object is the memory space of the subclass object, the subclass method public void print () it has been loaded into memory.
So we call the subclass method public void print (); if you need to call the override method of the parent class, you need to use super.
This section explains Test 2.
Summary:
Here is my opinion:
Assign a subclass object to the parent class object. The obtained object is such an object:
It is a parent class object for compilation, but the running is a subclass object. The specific features are as follows.
1. declared as a parent class Object
2. attributes of the parent class
3. Use the sub-class memory space
4. When the subclass method overwrites the method of the parent class, the object calls the subclass method. Otherwise, the method inheriting the parent class is automatically called.
5. I think this object is neither a parent class object nor a subclass object. When we use its method,
I think of it as a subclass object. If I use its attributes, I think of it as a parent class object.

It is an object that occupies the attributes of the parent class and uses the subclass method. as for what the object is, I think it should be declared as a parent class object, but it has a subclass method.
     
Think about it: Based on Test 3, how do we retrieve the attributes of sub-classes?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.