Understanding polymorphism and understanding parent class references pointing to subclass objects

Source: Internet
Author: User

Suppose there is a parent class father, And the variables in it need to occupy 1 MB of memory. There is a sub-class son, and the variables in it need to occupy MB of memory.

Now we can use the code to check the memory allocation:

Father F = new father (); // The system will allocate 1 MB of memory.

Son S = new son (); // The system will allocate MB of memory.


Because there is a hidden reference in the subclass that points to the parent class instance, a parent class will be instantiated before the subclass is instantiated, that is, the constructor of the parent class will be executed first.

Because S contains the instance of the parent class, s can call the method of the parent class.


Son S1 = s; // S1 points to the M memory. (it can be understood that the subclass reference points to the subclass object)


Father F1 = (father) s; // at this time, F1 will point to the 1 m memory in m, that is, F1 only points to the parent Instance Object of the instance in S, therefore, F1 can only call the method of the parent class (stored in 1 MB memory), but cannot call the method of the subclass (stored in MB memory ).


Son S2 = (son) F; // classcastexception will be reported when the code is run. because F only has 1 MB of memory, and sub-class references must have MB of memory, it cannot be converted.


Son S3 = (son) F1; // This statement can be run. 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.


Example:

Class father {

Void print (){};

}


Class son extends father {

Void print () {system. Out. println ("subclass! ");}

Void show () {system. Out. println ("show! ");}

}


Class demo {

Public static void main (string ARGs []) {

Father OBJ = new son ();

OBJ. Print ();

OBJ. Show (); // this call will report an error!

}

}


1. If you want to implement polymorphism, you must have three conditions: parent class reference, subclass object, and method override.

If the fathor class has a show () method, then the method overwrites are formed. In this case, you can write: obj. Show (), which forms a polymorphism.

2. There is no method overwrite, so you can only interpret it as the parent class reference to access the method of a subclass. Of course, the parent class reference does not have such a wide range of permissions, and of course an error will be reported.

PS: polymorphism is actually a mechanism. At the time of compilation, a virtual table is generated to record all overwriting methods. The method that is not overwritten will not be recorded in this table.

If a parent class calls a subclass method that is not covered, it does not conform to the table, and an error will be reported during compilation.

During program execution, the virtual opportunity goes to this virtual table to find the overwriting method. For example, the reference actually contains a reference of a subclass object, then we will find the corresponding override method in the subclass for execution.

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 the attributes and methods defined in the parent class, but it is helpless for the methods defined in the Child class but not in the parent class;

At the same time, a method in the parent class can be referenced and called by the parent class only when it is defined in the parent class but not overwritten in the subclass;

Understanding of polymorphism: polymorphism is embodied in inheritance, so there must be an inheritance relationship. Then the subclass must override the parent class method, and finally the parent class points to the subclass (the parent class has some methods, these methods are overwritten by the quilt class, but when these methods are called, The subclass override will be automatically called ).

Polymorphism is embodied in rewriting and overloading. polymorphism is a variety of Expression Methods of classes, such as different parameters with the same name, and subclasses rewrite the parent class.

Take a look at the following program:

Class father {

Public void func1 (){

Func2 ();

}


// This is the func2 () method in the parent class, because this method is rewritten in the subclass below

// This method will no longer be valid when called in the reference of the parent class type

// Replace it with the func2 () method that is rewritten in the call subclass.

Public void func2 (){

System. Out. println ("AAA ");

}

}

Class Child extends father {

// Func1 (int I) is an overload of the func1 () method.

// Because this method is not defined in the parent class, it cannot be referenced and called by the parent class.

// In the following main method, child. func1 (68) is incorrect.

Public void func1 (int I ){

System. Out. println ("BBB ");

}


// Func2 () overrides the func2 () method in the parent father class.

// If the func2 () method is called in the reference of the parent class type, this method must be rewritten in the subclass.

Public void func2 (){

System. Out. println ("CCC ");

}

}


Public class polymorphismtest {

Public static void main (string [] ARGs ){

Father child = new child ();

Child. func1 (); // what will be the printed result?

}

}



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. Func1 (int
I) 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 func1 (int
I) 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?

Obviously, it should be "CCC ".







The variable is not overwritten!

Public Class A {int A = 1 ;}

Public Class B extends a {int A = 2 ;}


The void compare (){

If (Super. A = This.)

System. Out. println ("not overrided ");

Else

System. Out. println ("overrided ");}

The console comes out with overrided

There is no polymorphism in the attributes in the class, that is, when you reference the above attributes, the system will only find the attribute in the referenced static type, but it has nothing to do with its actual type.

Static methods do not have polymorphism.

Understanding polymorphism and understanding parent class references pointing to subclass objects

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.