Dynamic examples in Java

Source: Internet
Author: User

// I will explain the dynamic implementation problem in Java with examples below
Class base
{
Public int ageofbase = 10;
 
Public void display ()
{
System. Out. println ("Display () in base! ");
}
 
Public void display1 ()
{
System. Out. println ("display1 () in base! ");
}

Private void show ()
{
System. Out. println ("show () in base! ");
}
}

Class test extends Base
{
Public int ageoftest = 10;
 
/* Rewrite the parent class method of this function.
However, he does not simply complete his functions;
While implementing the functions that should be implemented, it also retains the features of the parent class.
*************************************
What's important is that if you haven't rewritten this parent class method,
This method still exists in the subclass test, but it only exists.
It actually inherits from the parent class, but simply implements the parent class.
.
*/
Public void display ()
{
System. Out. println ("Display () in test! ");
Super. Display ();
}
 
// Note that the display1 () method is not rewritten, but this method is inherited.
// The subclass test still exists.
 
// Write a new method as a subclass.
Public void display2 ()
{
// Reference the private method of the parent class here
// This method does not exist in the subclass
// But remember:
// Private members accessing the parent class are not allowed
// Super. Show ();

// Reference the common methods of the parent class
// Actually, the reference here is the method in the subclass.
// Because this method has inherited from the parent class
Display1 ();

// This can also be referenced below
// Here is the method that actually references the parent class
// Super. display1 ();


// The above may not be clear.
// Another example is provided below.

// This is a subclass
Display ();

// This is the method of the parent class
Super. Display ();

System. Out. println ("display2 () in test! ");
}
 
Public static void main (string ARGs [])
{
Test T = new test ();
Base B = new base ();
// The following sentence is the root of polymorphism.
// Reference a parent class to a subclass
B = T;

// Call the method through the reference of the parent class.
// Actually the method implemented in the execution subclass

/*
Next, let me talk about my experience and views on object-oriented programming.
In the meantime, I cannot guarantee that everything I said is correct, just for your reference.

Polymorphism is an important concept in the object-oriented programming field. In combination with C ++ and Java
To describe the actual situation.

(1) it is best to have a certain understanding of virtual pointers and virtual pointer tables.
If you don't know, you can only remember what I said, but you may not understand it very well.

(2) The parent class references the Child class. When a method is called, the sub-class method is executed.
This statement has many prerequisites and exceptions.
Here is an example.
*/

B. Display ();

// **************** The original article has an error description **************** *******
// The following call will result in an error ()

// T. display2 ();

// ******************** End of incorrect original expression ************** *****

// Change it to the following description.

// The following line should be changed to B. Display ()

// This can be combined with the example in this article. At the same time, the following line can be run.

// The author solemnly apologizes for such mistakes

//************************************** **********************

// It should be as follows
(TEST) B). display2 ();

Int temp1 = B. ageofbase;

// The following call will cause an error
// Int temp2 = T. ageoftest;
// It should be as follows
Int temp2 = (TEST) B). ageoftest;


/*
Now, let's explain the two causes of errors above. This is the root cause of dynamic nature.
I try my best to explain it clearly.

* ************ Key 1 ********************

Because the size of the parent class and subclass objects in the memory is different, of course
Different objects cannot belong to the same memory address.
This should be okay, of course, if the subclass is just simple
It may be the same if it is not inherited, but in C ++
There may be some differences between compilers, and they may be different.

*********************** *

References are implemented by pointers. A deeper point is that references are pointers to pointers,
The memory size managed by different types of pointers is different from the above.
Very tight contact. Note that the memory for pointer management is not a pointer.
The pointer must be 4 bytes in length. Here we can simply say that the visibility of Variables
The problem is that although the variable exists, it is not necessarily visible.

* ************* Key 3 *********************** *

The key to polymorphism is dynamic binding. To put it bluntly, virtual function virtual pointer table
And other important concepts. To put it simply, this is complicated, but every real
Object-oriented programmers must take time to understand this. Only static functions are available in Java.
And virtual functions. c ++ also has a non-static non-virtual function. So Java's dynamic
Binding is easier. You can not understand this accurately (because the explanation is accurate, I am also very lazy,
Can write a long article). A virtual function table is generated every time the class is compiled,
The table stores all the virtual function addresses in its own class, and if this class has a parent class
(Whether it is the extends class or implements Interface ),
The virtual function tables inherited from the parent class are also inherited (here I am just a rough explanation, not very accurate ).
Note:
**************************************** ********
If the subclass does not override the method inherited by the parent class, the corresponding virtual function address in the parent class virtual function
The virtual function address of the Child class. If it is not changed, the inherited virtual function address does not need to be changed.
The address of the function corresponding to the virtual function table.
**************************************** *********

*********************** *
When the parent class references a subclass object, the parent class references the managed memory space,
It is smaller than the actual memory space of the subclass. So the subclass has more
Things are invisible to this parent class reference. So there is no way to reference it.

The above variable reference and function reference are like this, because the parent class does not
Variable ageoftest and function display2. But we can know that the parent class pointer
It actually points to the subclass, so our solution to this problem is:
Forced conversion type (styling in Java ).

Someone may ask, isn't the function address in the subclass also in the virtual function table?
I'm glad you asked this question, but the virtual function table shown in the parent class reference does not.
The function project, because it is absolutely invisible to the parent class reference.

For another virtual function call, if you read the instructions carefully, you must
Although the parent class references a small management memory range, but the virtual function table, which exists in the parent class,
However, since the function quilt class is rewritten, the function address programming subclass in that table is pulled,
Of course, it is the method for referencing subclass.


********************* ***
In Java, static functions are not dynamic, because they cannot be inherited or inherited.
It is rewritten as a non-static method. Static means that all instances of the class share an object.
**************************************** **********
*/
}
}

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.