Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of upward transformation of Java polymorphism?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the principles related to the upward transformation of Java polymorphism, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Polymorphism is the third major feature of object-oriented.

The advantages of polymorphism

Improve the organization and readability of the code. Ability to create extensible programs. (new features can be added at any time) eliminate coupling between types.

To tell you the truth, as a rookie, I can not quite understand the above three advantages. With the in-depth study, the understanding should be deeper and deeper.

Upward transformation

Concept

Java allows the subclass object to be assigned to the reference variable of the parent class without any cast, and the system completes it automatically. The upward transformation comes from the bottom-up inheritance relationship, the subclass inherits the parent class, and the subclass is a special parent class, so the operation of upward transformation is reasonable.

Here is a simple code to try to understand the concepts and benefits of upward transformation.

Package com.my.pac14;/** * @ auther Summerday * / public class DynamicBinding {/ / Object is the superclass of all classes. Depending on the upward transformation, this method can accept any type of object public static void test (Object x) {System.out.println (x.toString ());} public static void main (String [] args) {test (new PrimaryStudent ()); / / Student test (new Student ()); / / Student test (new Person ()) / / Person test (new Object ()); / / java.lang.Object@1b6d3586}} class Person extends Object {@ Override public String toString () {return "Person";}} class Student extends Person {@ Override public String toString () {return "Student";}} class PrimaryStudent extends Student {}

We can see that the following method receives an object of type Object and calls the object's toString () method.

Public static void test (Object x) {System.out.println (x.toString ());}

The following is the invocation statement, except for the fourth sentence, the other incoming objects do not seem to match the formal parameter type, but of course they can be run, which implies what we call an upward transformation.

Public static void main (String [] args) {test (new PrimaryStudent ()); / Student test (new Student ()); / / Student test (new Person ()); / / Person test (new Object ()); / / java.lang.Object@1b6d3586}

Take the object passed in the Student type, for example, to disassemble it, it is the following expression:

Object x = new Student ()

The Object class is the superclass of all classes, and the subclass type object created in the above formula is directly assigned to the reference variable of the parent class type, which is allowed in Java, which is called upward transformation. The reason why it can be implemented is also because the subclass may shrink the interface in the process of upward transformation, but at least it will not be narrower than some interfaces in the parent class.

To take a simple example, suppose that human beings can be divided into many kinds. We can say that students are a kind of human beings, but we cannot say that human beings are a kind of students. To a certain extent, the upward transformation allows the subclass to extend the partial loss of the superclass, which can only be achieved by calling the methods in the parent class through the parent class reference variables. when we manipulate human beings, we can only choose among the behavioral attributes that human beings have. We can't operate it directly according to the standards of the student class, because we don't know which class he is, if he's not a student, right? it's always right to use human beings, because we have something human. There must be one in your student class. This is what I understand as upward transformation.

What's so good about the upward transformation?

If there is no upward transformation mechanism, we need to add a lot of overloaded test methods to achieve the original effect, which is too tedious. If you want to add a method like test () or add a new class derived from Object, you will do more complex operations, which is not good for extension and is not desirable.

/ / the original situation: you need to create a lot of test methods. Public static void test (Object x) {System.out.println (x.toString ());} public static void test (Person x) {System.out.println (x.toString ());} public static void test (Student x) {System.out.println (x.toString ());} public static void test (PrimaryStudent x) {System.out.println (x.toString ());}

The existence of polymorphism just solves this thorny problem. in order to facilitate extension, we only need to write a simple method that only accepts the base class as a parameter, and it is really comfortable to automatically select the method to call the corresponding derived class at run time, regardless of the exported class.

After reading the above, do you have any further understanding of the principles related to the upward transformation of Java polymorphism? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report