Talking about assigning the subclass object to the parent class object _java programming

Source: Internet
Author: User

Recently I have a little experience in assigning a subclass object to a parent object, and I'd like to share it with you. But I have a limited level, please correct me and criticize you.
To go to the point, here are a few small examples, please take a look.
  
  Test A

  Parent class:


public class Supclass
{
public void print ()
{
System.out.println ("This Is parent class print () method" + "--At this point object" +this.tostring ());
}
}


Sub class:


public class subclass extends Supclass
{
public static void Main (string[] args)
{
Supclass Sup=new Subclass ();
Sup.print ();
System.out.println ("Object at this Time" +sup.tostring ());
}
}


Result: This is parent class print () method-the object is subclass@126b249
At this point the object is subclass@126b249

Description
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 invoked, and the output is the subclass object name resolution.

Conclusion: The object declared at compile time is the parent class object, but the runtime is a subclass object. A subclass does not have a method that overrides the parent class, then the object at this time invokes the method that inherits the parent class.

 Test Two

Parent class:


public class Supclass
{
public void print ()
{
System.out.println ("This Is parent class print () method" + "--At this point object" +this.tostring ());
}
}

Sub class:


public class subclass extends Supclass
{
public void print ()
{
System.out.println ("This is subclass print () method" + "-At this point object" +this.tostring ());
}
public static void Main (string[] args)
{
Supclass Sup=new Subclass ();
Sup.print ();
System.out.println ("Object at this Time" +sup.tostring ());
}
}


Result: This is subclass print () method-the object is subclass@126b249
At this point the object is subclass@126b249
Description
I have overridden the print () method of the parent class, based on the previous example, where the print () method of the subclass is invoked.

Conclusion: On the basis of the last example conclusion, I got a conclusion: at this time the object runtime is really a subclass object, if the subclass does not have the method of writing the parent class,
The object at this time invokes the method that inherits the parent class, otherwise, the object at this time invokes the subclass method.

Question: Can we conclude from the above test that the subclass object is assigned to the parent class object (that is, Supclass Sup=new subclass ()),
What we get is the subclass object, that is, the SUP is the subclass object??????

   Test Three

Parent class:


public class Supclass
{
Protected String Classname= "parent class Property";
public void print ()
{
System.out.println ("This Is parent class print () method" + "--At this point object" +this.tostring ());
}
}


  Sub class:


public class subclass extends Supclass
{
Protected String classname= "Subclass Property";
public void print ()
{
System.out.println ("This is subclass print () method" + "-At this point object" +this.tostring ());


}
public static void Main (string[] args)
{
Supclass Sup=new Subclass ();
System.out.println ("The property at this time:" +sup.classname);
}
}

Result: The property at this time: Parent class Property

Description: I added a property classname to the parent class on the basis of the first test, overriding this attribute in subclasses.
But when I output the properties of the object at this time, it is the property of the parent class.
Conclusion: The subclass object is assigned to the parent class object, and methods and properties are very different from our Orthodox inheritance relationship.
Problem:
Is the object a subclass object or a parent class object at this point?

  To begin to speculate:

Before I speculate, there are a few points to be stated:

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

The object in 2.java is not a complete object-oriented idea, that is, not to encapsulate an object's properties and methods in the object.
Instead, the object has its own properties, but it refers to the method in the class, which is to encapsulate the reference of the method in the property and the class to the object.
The method that the object invokes is not its own method, but the method in the class. As for Java Why do this, I do not know.

3. When an object is loaded into memory, the class is first loaded into memory, and thereafter the class should be left in memory. As to when the class disappears from 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 thing to do when compiling is declaring the type of object, assigning attributes, checking syntax errors, etc.
The runtime does this by loading the object into memory (commonly used in new, reflective), running code execution functions, and so on.
  
5. If the reader you and I do not resonate in these points, or if we do not have the same knowledge in these points, you will think I'm talking nonsense.
Perhaps you will think that my experts are too low, I feel that my credibility is low. But I would like to say that learning is not in order, the first.
Well, I'm ready to turn my expertise into a negative, no nonsense, we go on.

Speculate:
1. When we compile Supclass sup=new subclass (), the SUP object is declared as a Supclass class, and the properties of the SUP object are the values of the properties of the parent class object.

It is OK at compile time. (Statement 4, this paragraph can be explained). This passage can explain the test three, that is, why the property of the object at this time is the property 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 (statement 4, which can be interpreted).
Note that the memory space of the SUP object now consists of the necessary information and subclass object information of the two parts of the parent class (statement 1, which can be interpreted).
When we do not rewrite the method of the parent class, the method of inheriting the parent class, public void print () can certainly be invoked because the memory space already has some necessary information for the parent class.
This passage can explain the test.

3. Then 2 goes on, and if we rewrite the method of the parent class, the method public void print () of the child class is loaded into memory because the memory space of the SUP object is the memory space of the subclass object.
So we call the subclass's method public void print (), and if you need to invoke the overridden method of the parent class, you need to use super.
This passage can explain test two.   
Summary:
The following are purely personal:
Assign a subclass object to a Parent object:
It is a compilation is a parent object, but it is run as a subclass object, as follows.
   1. is declared as the parent class object
2. Owns the parent class Property
3. To occupy the memory space of a subclass
4. When a subclass method overrides a method of a parent class, the object invokes the method of the subclass, otherwise the method that inherits the parent class is invoked automatically.
   5. I think this object is neither a parent object nor a subclass object. When we use its method,
I see it as a subclass object, and if I use its properties, I see it as a parent object.
It is an object that occupies the parent class property and uses the subclass method. As to what exactly is the object, I think still have to according to the Declaration, it should be regarded as the parent class object, but have the subclass method.

Think about it:
on the basis of test three, how do we take the attributes of a subclass?????

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.