Java Basic Tutorial - Polymorphism

Intuitively speaking, polymorphism is "one object, many forms". For example, there are many forms of Guan Shiyin Bodhisattva.——

Everyone has many forms.——

Specifically, polymorphism refers to "the same object, the same method (function), showing different behavior".
At the code level, the parent object points to the subclass instance, and the parent object calls (multiple) subclass methods. For example:

List lst; // lst is defined in List form
lst = new ArrayList();// When instantiated, it's subclass objects
lst = new LinkedList(); // Can be different subclass objects

The underlying technology: late binding (dynamic binding / runtime binding), which dynamically determines the method to be invoked based on runtime object instances.

Example: Driving

Generally speaking, the driver's license is C1, which can drive many kinds of small cars.

Now we define a car class, two subclasses are "minivan" and "triple jumper".
The main method in the main class "driver's license" can create two subclasses of car objects and call their "driving ()" methods respectively. The code is as follows——

public class driver's license{
    public static void main(String[] args) {
        Minivan b = new minivan ();
        b. Driving ();
        Triple jumper c = new triple jumper ();
        c. Driving ();
    }
}
class car{
    public void driving (){
        System.out.println("startup theory");
    }
}
class minivan extends{
    public void driving (){
        System.out.println("minivan");
    }
}
class triple jumper extends{
    public void driving (){
        System.out.println("triple jumper");
    }
}

In the example above, two objects, b and c, are defined. It can be understood that driving a pickup truck needs to know the pickup truck first, and driving a triple jumper needs to know the triple jumper first, which is a bit troublesome.
Can you take a picture and drive freely?

    public static void main(String[] args) {
        // Minivan b = new minivan ();
        // b. Driving ();
        // Triple jumper c = new triple jumper ();
        // c. Driving ();
        Automobile c;
        c = new van ();
        c. Driving ();
        c = new triple jumper ();
        c. Driving ();
    }

The modified code declares only one object c, which belongs to the instance object of the parent class, and can be instantiated into subclasses separately. Although method calls are all "c. drive ()", the output is different. This is polymorphism.

Upward Transition, Downward Transition

Instantiating a subclass object in the name of the parent class belongs to the upward transition.
The upward transformation is an automatic promotion. Because the upper level is more abstract and the scope is larger, there is no problem.

The pickup truck is also a car. It is no problem to represent the lower level with a more abstract upper level.
But if the car is a pickup truck, it is not necessarily right. This belongs to the downward transformation.

The downward transition requires mandatory conversion. Because the lower level is more specific, there may be undefined methods of the parent class, and transition is not necessarily safe.

The following code demonstrates the mandatory downward transition with parentheses——

public class Polymorphism transition {
    public static void main(String[] args) {
        Animal _animal = null;
        Cat _cat = new Cat();
        _animal = _cat;// Upward Transition (Autoelevation): Cats are animals, no problem.
        _cat = (Cat) _animal;// Downward Transition (Forced Transition): Animals are not necessarily cats
        // _The reason why the downward transition is successful is that _animal is Cat originally and belongs to reduction.
        // -----------
        // Animals may not be cats, transformation may be wrong
        _animal = new Dog();
        _cat = (Cat) _animal;// ClassCastException
        // Even simple parent instance objects fail downward transition
        _animal = new Animal();
        _cat = (Cat) _animal;// ClassCastException
    }
}
class Animal {
}
class Cat extends Animal {
}
class Dog extends Animal {
}

Another example: String class is a subclass of Object

        String s ="Lord Lao Zi";
        Object o = s;// Upward Transition (Automatic Upgrading)
        s = (String)o;// Downward Transition (Coercive Conversion)

instanceof operator

Used to determine whether the object on the left belongs to the type on the right (instance, subclass instance).
If there is no inheritance relationship between the left object and the right class, the compilation will go wrong.

public class Polymorphism transition {
    public static void main(String[] args) {
        Animal _animal = new Cat();
        System.out.println(_animal instanceof Cat);
        System.out.println(_animal instanceof Animal);
        System.out.println(_animal instanceof Dog);
        System.out.println(_animal instanceof Object);
        // There is no inheritance relationship with String class and compilation cannot pass
        // System.out.println(_animal instanceof String);
        // ----------------------
        // First judge, then transform
        if (_animal instanceof Cat) {
            Cat _cat = (Cat) _animal;
            System.out.println(_cat.getClass());
        }
    }
}
class Animal {
}
class Cat extends Animal {
}
class Dog extends Animal {
}

Result:

true
true
false
true
class Cat

Keywords: PHP

Added by j115 on Fri, 12 Jul 2019 21:42:54 +0300