Simply understand the principles of Java polymorphic upward transformation

Time:2021-8-24

This article mainly introduces the principles related to the simple understanding of Java polymorphic upward transformation. It is introduced in great detail through the example code, which has a certain reference value for everyone’s study or work. Friends in need can refer to it

Polymorphism is the third feature of object-oriented.

Advantages of polymorphism

  • Improve code organization and readability.
  • Ability to create extensible programs( Ready to add new features)
  • Eliminate coupling between types.

To tell the truth, as Xiaobai, I don’t quite understand the above three advantages. With in-depth study, understanding should be deeper and deeper. Encourage each other.

Upward transformation

concept

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

Let’s try to understand the concept and benefits of upward transformation according to the simple code.

package com.my.pac14;

/**
 * @auther Summerday
 */
public class DynamicBinding {
  //Object is the superclass of all classes. According to 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[email protected]
  }
}

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 toString () method of the object.


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

The following is the call statement. Except for the fourth sentence, other incoming objects look inconsistent with the formal parameter type, but of course they can run. This contains what we call upward transformation.


public static void main(String[] args) {
  test(new PrimaryStudent());//Student
  test(new Student());//Student
  test(new Person());//Person
  test(new Object());//[email protected]
}

Take the object of student type passed in as an example. After disassembly, it is the following expression:


Object x = new Student();

The object class is a superclass of all classes. In the above formula, the created subclass type object is directly assigned to the reference variable of the parent class type, which is allowed in Java. This is the so-called upward transformation. The reason why it can be implemented is that in the process of upward transformation, the subclass may narrow the interface, but at least it will not be narrower than some interfaces in the parent class.

For a simple example, assuming that human beings can be divided into many, many kinds, we can say that students are a kind of human beings, but we can’t say that human beings are a kind of students. To a certain extent, the upward transformation allows the subclass to extend and part of the superclass to be lost. Through the parent class reference variable, we can only call the methods in the parent class. When we operate human beings, we can only choose from the behavior attributes of human beings, and we can’t directly operate it according to the standard of student class, because we don’t know which class it is. In case it’s not a student, right, It’s always right to use human beings, because I have something human beings, your students must have. This is what I understand as upward transformation.

What’s good about upward transformation

If there is no upward transformation mechanism, we need to add many overloaded test methods to achieve the original effect, which is too cumbersome. If you want to add a method like test () or add a new class exported from object, you will also do more complex operations, which is not conducive to expansion and is not desirable.

//Original situation: a lot of test methods need to be created.
  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 expansion, you only need to write a simple method that only receives the base class as parameters. No matter how the exported class is, it is really comfortable to automatically select and call the method of the corresponding exported class at runtime.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]