Parent class and subclass conversion (assign a subclass object to the parent class Object)

Source: Internet
Author: User

Parent class and subclass conversion (assign a subclass object to the parent class Object)

I recently learned a little about assigning a subclass object to a parent class object. I 'd like to share it with you. However, my skills are limited. Please correct and criticize me.
Let's take a look at the following examples.
 
Test 1
Parent class:

  1. Public ClassSupclass
  2. {
  3. Public VoidPrint ()
  4. {
  5. System. Out. println ("this is the print () method of the parent class" + "-- the object" +This. ToString ());
  6. }
  7. }

Subclass:
  1. Public ClassSubclassExtendsSupclass
  2. {
  3. Public Static VoidMain (String[] Args)
  4. {
  5. Supclass sup =NewSubclass ();
  6. Sup. print ();
  7. System. Out. println ("Current object" + sup. toString ());
  8. }
  9. }

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:
  1. Public ClassSupclass
  2. {
  3. Public VoidPrint ()
  4. {
  5. System. Out. println ("this is the print () method of the parent class" + "-- the object" +This. ToString ());
  6. }
  7. }

Subclass:
  1. Public ClassSubclassExtendsSupclass
  2. {
  3. Public VoidPrint ()
  4. {
  5. System. Out. println ("this is subclass print () method" + "-- Object" +This. ToString ());
  6. }
  7. Public Static VoidMain (String[] Args)
  8. {
  9. Supclass sup =NewSubclass ();
  10. Sup. print ();
  11. System. Out. println ("Current object" + sup. toString ());
  12. }
  13. }

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:
  1.  Public ClassSupclass
  2. {
  3. Protected StringClassName = "parent class attributes ";
  4. Public VoidPrint ()
  5. {
  6. System. Out. println ("this is the print () method of the parent class" + "-- the object" +This. ToString ());
  7. }
  8. }

Subclass:
  1. Public ClassSubclassExtendsSupclass
  2. {
  3. Protected StringClassName = "subclass property ";
  4. Public VoidPrint ()
  5. {
  6. System. Out. println ("this is subclass print () method" + "-- Object" +This. ToString ());
  7. }

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.