Java Basic Grammar - Polymorphism

Basic Introduction

Polymorphism: Multiple states.

Method or object has many forms, the third feature of object-oriented.

Polymorphism is based on encapsulation and inheritance.

Reflect Specifically

Polymorphism of methods

Both overrides and overloads of methods can be polymorphic

Example:

public class PolyMethod {
    public static void main(String[] args) {
        //METHOD REPLICATION POLYMORPHISM
        A a = new A();
        //If you invoke the sum method with a different number of arguments, you invoke a different method
        //For the sum method, it is the representation of polymorphism.
        System.out.println(a.sum(10,20)); //Call the sum method of two parameters
        System.out.println(a.sum(10,20,30)); //Call the sum method with three parameters

        //Method overrides reflect polymorphism
        B b = new B();
        a.say();//Call the say method inside class A
        b.say();//Call the say method inside class B
    }
}

class B{//Parent Class
    public void say(){
        System.out.println("B say() Method Called...");
    }
}

class A extends B{//Subclass
    public int sum(int n1,int n2){ //And the following sum to form an overload
        return n1 + n2;
    }

    public int sum(int n1,int n2, int n3){
        return n1 + n2 + n3;
    }

    public void say(){
        System.out.println("A say() Method Called...");
    }
}

Result:

30
60
A say() Method Called...
B say() Method Called...

Object Polymorphism [Core]

  • The compilation and run types of an object can be inconsistent
  • The compilation type is determined when the object is defined and cannot be changed
  • Run type is changeable
  • Compile type to define = left of sign, run type to define = right of sign
Animal animal = new Dog();#The animal compilation type is Animal and the run type is Dog
animal = new Cat();#The animal run type becomes Cat, and the compile type remains Animal

That is, a reference to a parent class can point to an object of a subclass.

Example:

public class Animal {
    public void cry(){
        System.out.println("Animal cry()");
    }
}
public class Cat extends Animal {
    public void cry(){
        System.out.println("Cat cry() kitten");
    }
}
public class Dog extends Animal{
    public void cry(){
        System.out.println("Dog cry() puppy");
    }
}
public class PoluObject {
    public static void main(String[] args) {
        //Experience the characteristics of object polymorphism
        //Animal compilation type is Animal run type is Dog
        Animal animal = new Dog();
        //At run time, go to the next line, animal runs as Dog, so cry is the cry in the Dog class
        animal.cry();
        //At this point, animal's compilation type is Animal and its run type becomes Cat
        animal = new Cat();
        //When running to the next line, animal runs as Cat, so cry is the cry in the Cat class
        animal.cry();

    }
}

Cat and Dog classes inherit the Animal class.

Result:

Dog cry() puppy
Cat cry() kitten

Owner Feeding Problems

Title: Owners need to feed cats fish and dogs bones every day, so how do we use java code to reflect the owner's actions?

No polymorphism is used:

An animal class, Dog and Cat, inherit the animal class

public class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }
}

public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
}

One food class, Fish and Bone, inherits the Food class.

public class Food {
    private String name;

    public Food(String name) { //This constructor overrides the default parameterless constructor
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Fish extends Food {
    public Fish(String name) {
        super(name);
    }
}

public class Bone extends Food{
    public Bone(String name) {
        super(name);
    }
}
public class Master {
    private String name;

    public Master(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //The owner feeds the puppy bones
    public void feed(Dog dog, Bone bone){
        System.out.println("master" + name + "to" + dog.getName() + "eat" + bone.getName());
    }

    //Owner feeds cat fish, method overload
    public void feed(Cat cat,Fish fish){
        System.out.println("master" + name + "to" + cat.getName() + "eat" + fish.getName());
    }
}

As you can see, when polymorphism is not used, both the way the owner feeds the dog bones and the cat fish are written out. Imagine that if the owner feeds the animal more and more, we need more and more overloaded feeding methods, which are less friendly to the reusability and maintainability of the entire code.

public class Poly01 {
    public static void main(String[] args) {
        Master tom = new Master("tom");
        Dog bigye = new Dog("bigye");
        Bone bone = new Bone("bone");

        tom.feed(bigye,bone);
    
        Cat cat = new Cat("cat");
        Fish fish = new Fish("fish");
        tom.feed(cat,fish);
    }

}

Result;

master tom to bigye eat bone
 master tom to cat eat fish

Using polymorphism:

public class Master {
    private String name;

    public Master(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //Use polymorphic mechanisms to manage master feeding issues
    //The animal compilation type is Animal and can point to objects of the Animal subclass
    //The food compilation type is Food and can point to objects of the Food subclass
    public void feed(Animal animal, Food food)
    {
        System.out.println("master" + name + "to" + animal.getName() + "eat" + food.getName());
    }
//    //The owner feeds the puppy bones
//    public void feed(Dog dog, Bone bone){
//        System.out.println("Master" + name + "To" + dog.getName() + "Eat" + bone.getName());
//    }
//
//    //Owner feeds cat fish, method overload
//    public void feed(Cat cat,Fish fish){
//        System.out.println("master" + name + "to" + cat.getName() + "eat" + fish.getName());
//    }
}

With polymorphism, we can easily solve problems with overloaded methods by pointing animal to objects in the Animal subclass and food to objects in the Food subclass.

Result:

master tom to bigye eat bone
 master tom to cat eat fish

You can see that the results are exactly the same, but with polymorphism, you can improve the reusability and maintainability of your code.

Polymorphic considerations and details

Prerequisite: There is an inheritance relationship between two objects or classes.

Polymorphic Upward Transition: References to parent classes point to objects of subclasses. [Essential]

Syntax: parent type reference name = new subclass type ();

Features: Compile type = Left, Run type = Right

You can call all members in the parent class, not specific members in the subclass. The final result depends on the implementation of the subclass.

Keywords: Java

Added by mits on Sun, 19 Sep 2021 20:13:13 +0300