Learn more about parent class reference pointing to subclass

Source: Internet
Author: User

The parent class application points to a subclass object and refers:

The parent class animal, subclass cat, dog. Here, animal can be an interface or class, and cat and dog are subclasses that inherit or implement animal.

Animal animal = new CAT ();

The declared parent class actually points to the subclass object. Let's first understand from the memory perspective.

Assume that the aninal parent class uses 1 MB for its variables, and its subclasses dog and cat need to use MB of memory.


View memory allocation through code:

Animal animal = newanimal (); // The system will allocate 1 MB of memory

Catcat = new CAT (); the system will distribute MB of memory! Because the subclass has a hidden reference, super points to the parent class instance. Therefore, a parent class will be instantiated before the subclass is instantiated. Execute the parent class constructor first. Because the instance contains the parent class, s can call the method of the parent class.

Cat cat1 = cat, pointing to the 1.5m memory

Animal animalone = (animal) CAT; // at this time, animalone points to 1 MB memory in MB, while animalone only points to the parent class instance object of the cat instance. Therefore, the animalone function calls the method of the parent class and cannot call the subclass method (stored in MB memory). Cat cat2 = (CAT) Animal // classcatexception is reported during running. Because animal only has 1 MB of memory, and sub-class references must have MB of memory, it cannot be converted.

Cat cat3 = (CAT) animalone; // This statement can be run. At this time, S3 points to the MB memory. because F1 is converted from S, it has MB of memory, but it only points to 1 MB of memory.

The above is analyzed from the memory perspective.


From the object perspective

Let's take a look at several keywords:Polymorphism, dynamic link, and upward Transformation

Encapsulation: Hides the internal implementation mechanism of the class. You can modify the internal structure of the class without affecting the user and protect the data;

Inheritance: To reuse the parent class code, the Child class inherits the parent class and has members of the parent class.

Method rewriting, overloading, and dynamic connectionPolymorphismSex

To understand polymorphism, you must first understand "upward transformation" (subcategories are converted to parent classes ).

Defines a subclass cat, which inherits the animal class, and the latter is the parent class. You can use catc = new CAT (); to instantiate a cat object, which is not hard to understand.

But when I define it like this: Animal A = new CAT (); what does this mean?

It indicates that I have defined an animal type reference pointing to the newly created cat type object. Since cat inherits its parent class animal, the reference of the animal type can point to the cat type object. This is "upward transformation ".

So what is the significance of this?

Because subclass is an improvement and extension of the parent class, it is generally more powerful than the parent class in terms of functionality and has more unique attributes than the parent class, defining a reference of a parent class pointing to an object of a subclass can both use the powerful functions of the subclass and extract the commonalities of the parent class. Therefore, the reference of the parent class can call all attributes and methods defined in the parent class. For methods defined in the subclass but not in the parent class, the parent class reference cannot be called;

◆ What is dynamic link?

When the subclass of the parent class method is override, the parent class is called. If the subclass overrides the parent class method, the subclass method is called. This is a dynamic connection.

Class father {public void func1 () {func2 ();} public void fun2 () {system. out. println ("AAA") ;}} class Child extends father {publlic void func1 (int I) {system. out. println ("BBB");} public void func2 () {system. out. println ("CCC") ;}} public class test {public static void Mian (string [] ARGs) {FATHER child = new child (); child. func1 (); // What will be the result? Child. func1 (89 );}}

The above program is a typical example of polymorphism. Child child inherits the parent class father, reloads the func1 () method of the parent class, And overwrites the func2 () method of the parent class. The overloaded func1 (INTI) and func1 () are no longer the same method. Because the parent class does not have func1 (int I, the reference child of the parent class cannot call the func1 (INTI) method. The subclass overrides the func2 () method, so the reference child of the parent class will call func2 () in the subclass when calling this method ().

So what kind of results will the program print? Apparently,It should be"CCC".


Summary:

Through the above two methods, we understand that the parent class references the Child class, which is actually a multi-state application, reflected in the following points:

  • Use the reference of the parent class to point to the object of the subclass.
  • This reference can only call methods and variables defined in the parent class.
  • If a method in the parent class is rewritten in the subclass, the method in the subclass is called when the method is called. (dynamic connection and dynamic call)
  • Variables cannot be overwritten. The "Override" concept only applies to methods. If the "Override" is used to override the variables in the parent class in the subclass, an error is reported during compilation.

 

Learn more about parent class reference pointing to 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.