Java Dynamic Binding/polymorphism

Source: Internet
Author: User
Tags gety


classPoint {intx = 0, y = 0; Parent class member Var Public voidMoveintXinty) { This. x + =x; This. Y + =y; } Public intGetX () {returnx; } Public intGetY () {returny; }}classRealpointextendsPoint {floatx = 0.0f, y = 0.0f; The subclass member Var, with the same name as the parent class, hides the parent member Var, including access rights and types, whichever is the subclass. Public voidMovefloatXfloaty) The override (override) of the inherited parent class method constitutes an overload. This. x + =x; This refers to a reference to itself, and this.x represents the member Var of its own class This. Y + =y; } Public voidMoveintXinty) {//override of the parent class method, the number of overloaded method (overload) parameters that call the own class is different and the remainder is the same. Move ((float) x, (float) y); } Public intGetX () {return(int) Math.floor (x); Math.floor () is rounded down, directly capturing the fractional part after the decimal point. } Public intGetY () {return(int) Math.floor (y); }} Public classPolymorphism { Public Static voidMain (String []args) {Realpoint RP=NewRealpoint (); Create subclass object, subclass reference to sub-class object point P=RP; The parent class reference type, pointing to the Subclass object (P and RP pointing to the same chunk of memory) Casting Object Transformation Rp.move (1.71828f,4.14159f); Call the first method defined by the subclass P.move (1,-1);  The second method of calling the subclass definition, which is actually implemented, is implemented by the first method.              Show (p.x, P.Y); Displays the parent class member Var, the non-static member variable is loaded in the second phase of the class load, and there is no dynamic binding.              Show (rp.x, RP.Y);        Shows the subclass member Var show (P.getx (), p.gety ()); Call Subclass Getx () GetY (), the subclass method returns a subclass member Var, a member method (with inheritance, a parent class reference to a subclass object, and a rewrite) that implements dynamic binding.
Call the method that actually points to the object, that is, call Getx () in Realpoint, GetY ();
Show (Rp.getx (), rp.gety ()); Call child class member method} Public Static voidShowintXinty) {System.out.println ("(" + x + "," + y + ")"); } Public Static voidShowfloatXfloaty) {System.out.println ("(" + x + "," + y + ")"); }}
(0,0)(2.7182798,3.14159) (2,3)(2,3)
View Code

The source program, such as dynamic bind (polymorphic), is for non-static, non-final member methods that are hidden against member variables and static methods. (For and for only)

only override (override) is dynamically bound, hidden (that is, the subclass declaration and the parent class have the same type of member variable and static method, the subclass instantiation object calls the member variable and the static method through the object name, and the child class hides the parent class accordingly. Only the members of the subclass Var and static methods are shown). Hiding does not make dynamic bindings.

Memory Layout,

1, p, and RP point to the same subclass object.

2. Subclass inherits the parent class, that is, the child class object contains the parent class object.

3, dynamic binding: The parent class object's move () pointer to the parent class point () method, because there is inheritance, there is a parent class reference to the subclass object, there is a rewrite, Java will perform a dynamic binding mechanism, so that the source code in P.move (1,-1) called the subclass of the Move () method. Move () change the black line to the blue-pointing Move () method.

4, the parent class refers to the child class object, can only call methods of the subclass, cannot get the child class member variable (member var no dynamic binding, and casting qualified the parent class reference can only use methods and variables within the parent class obj). That is, P.getx () is called as a subclass method. P.x does not get the member Var of the subclass, which is the reason why the result first to second appears.

5, but the parent class refers to the child class object, calls the subclass method, the subclass method can manipulate the child class member variable, approximate (but not equal to) the parent class reference can get the subclass member Var (but not directly, but through the subclass method,  The subclass method provides an interface to modify the subclass member Var out-of-class. P.getx () p.gety () and GetX () GetY () is returned as a subclass of its own member Var. The result of the third to fourth line of the program results.

Java Dynamic Binding/polymorphism

Related Article

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.