Detailed explanation of upcast objects in JAVA

 Detailed explanation of upcasting objects in Java

The first is that the parameter of the method is the parent class object, whether it is feasible to pass in the subclass object,
and then lead to Parent p = new Children();
This code is not very understandable. In the process of google, it is
necessary to understand the upward transformation and lead to dynamic binding.
From dynamic binding, static binding leads to the

concept of program binding:
binding refers to the association between the invocation of a method and the class (method body) where the method is located. For java, binding is divided into static binding and dynamic binding; or called early binding and late binding

. Static binding:
The method has been bound before the program is executed, and it is implemented by the compiler or other linker at this time. . For example: C.
For java, it can be simply understood as the binding at the compile time of the program; here is a special point, only the methods in java are final, static, private and constructors are early binding

Dynamic binding:
late binding: according to specific objects at runtime type to bind.
If a language implements late binding, it must also provide some mechanism to determine the type of the object at runtime and call the appropriate method respectively. That is to say, the compiler still does not know the type of the object at this time, but the method invocation mechanism can investigate by itself and find the correct method body. Different languages ​​implement late binding differently. But we can at least think of it this way: they all insert some special type of information into the object.

The process of dynamic binding: the
virtual machine extracts the method table of the actual type of the object; the
virtual machine searches for the method signature;
invokes the method.

Summary about binding:
After understanding the concept of the three, it is obvious that we find that java belongs to late binding. In java, almost all methods are late bound, at runtime the dynamic binding method belongs to the subclass or the base class. But there are also special ones. Since static methods and final methods cannot be inherited, their values ​​can be determined at compile time, and they are pre-bound. A special point to note is that privately declared methods and member variables cannot be inherited by subclasses, and all private methods are implicitly designated as final (from this we can also know that one of the methods declared as final is to prevent The method is overridden, and the second is to effectively close the dynamic binding in java). The late binding in java is implemented by the JVM, we do not need to explicitly declare it, but C++ is different, we must explicitly declare that a method has late binding.

Upcasting or polymorphism in java is achieved with the help of dynamic binding, so if you understand dynamic binding, you can get upcasting and polymorphism.
As mentioned earlier, for the methods in java, except for final, static, private and constructor methods that are early bound, all other methods are dynamically bound. The dynamic binding typically occurs under the conversion declaration of the parent class and the subclass: for
example: Parent p = new Children();
The specific process details are as follows:
1: The compiler checks the declared type and method name of the object.
Suppose we call the xf(args) method, and x has been declared as an object of class C, then the compiler will enumerate all the methods named f in class C and the f method 2 inherited from the superclass of class C : Next the compiler checks the parameter types provided in the method call. If, among all methods named f, there is a parameter type that best matches the parameter type provided by the call, then the method is called, a process called "overload resolution" 
3: When the program runs and the method is called using dynamic binding, the virtual machine must call the version of the method that matches the actual type of the object pointed to by x. Assuming that the actual type is D (subclass of C), if class D defines f(String), then the method is called, otherwise, the method f(String) is searched in the superclass of D, and so on

. Several examples (examples are from the Internet):
Java code 
view plaincopy to clipboardprint? 
public class Father {   
  public void method() {   
    System.out.println("Father class method, object type: " + this.getClass());   
  }   
}   
     
public class Son extends Father {   
  public static void main(String[] args) {   
    Father sample = new Son();//   
    Upcast sample.method();   
  }   
}   
Declare the reference of the parent class, but the execution process The object of the subclass is called in the program. The program first looks for the method method of the subclass object, but it is not found, so it transforms up to the parent class to find the

Java code 
public class Son extends Father {   
  public void method() {   
    System.out.println("Subclass method, object type: " + this.getClass());   
  }   
     
  public static void main(String[] args) {   
    Father sample = new Son();//   
    Upcast sample.method ();   
  }   
}   

Since the subclass rewrites the method method of the parent class, according to the above theory, it is known that the method method of the subclass will be called to execute, because the subclass object has a method method and does not have an upward transformation to find

the previous theory. The binding rules of java have been mentioned. From this, it can be seen that when dealing with member variables in java classes, it is not run-time binding, but static binding in the general sense. So in the case of upcasting, the method of the object can find the subclass, and the properties of the object are still the properties of the parent class.
The code is as follows:
Java code 
public class Father {   
   
  protected String name="Father property";   
     
  public void method() {   
    System.out.println("Father class method, object type: " + this.getClass());   
  }   
}   
     
public class Son extends Father {   
  protected String name="   
     
  public void method() {   
    System.out.println("Subclass method, object type: " + this.getClass());   
  }   
     
  public static void main(String[] args) {   
    Father sample = new Son();/ /   
    Upcast System.out.println("Called member: "+sample.name);   
  }   
}   
 Conclusion, the called member is the property of the parent.
This result shows that the object of the subclass (by the reference handle of the parent class) calls the member variable of the parent class. So it must be clear that runtime (dynamic) binding targets only methods of objects.
Now try to call the member variable name of the subclass, what should I do? The easiest way is to encapsulate the member variable into a method getter form.
The code is as follows:
Java code 
public class Father {   
  protected String name = "Father property";   
  public String getName() {   
    return name;   
  }   
  public void method() {   
    System.out.println("Father class method, object type: " + this.   
  }   
}   
     
public class Son extends Father {   
  protected String name="Son property";   
     
  public String getName() {   
    return name;   
  }   
     
  public void method() {   
    System.out.println("Subclass method, object type: " + this .getClass());   
  }   
     
  public static void main(String[] args) {   
    Father sample = new Son();//   
    Upcast System.out.println("Called member: "+sample.getName());   
  }   
}   

Result: The property of the son is called

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325786678&siteId=291194637