Java polymorphism: a case study

Recently, it is found that the foundation is really important. For example, the polymorphic features in Java are difficult to understand and abstract concepts in learning. When you learn, you are confused, but later you will find that foundation plays an important role in the understanding of daily work.

Next, I will use a code example to recall and consolidate the concept and significance of polymorphism. Understanding polymorphism is an important step in object-oriented programming.

Let's warm up and see the difference between 1 and 2 when the mian function is called. This is the simplest little chestnut. an is equivalent to a reference (type), but it can represent multiple states.

Polymorphism (concept):

Polymorphism refers to multiple states of a reference (type) in different situations. It can also be understood that polymorphism refers to calling methods implemented in different subclasses through a pointer to the parent class..

Scenario assumptions:

A master has a cat and a dog, both of which have their own favorite food. When the master feeds them, it is very troublesome to judge whether they are cats or dogs and what they like to eat respectively. If the owner keeps many kinds of animals, such repeated judgment will waste a lot of time. If there is any way to let the owner get a kind of food, he will know what kind of animal it is.

A complete code example:

1. First, create animals:

// Zoology
class Animal {
    int age;
    String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    // There are two ways to call and eat in animals
    public void cry() {
        System.out.println("I don't know what it's called");
    }

    public void eat() {
        System.out.println("I don't know what to eat");
    }
}

2. Second, create cats and dogs (they inherit from animals):

// Dogs inherit from animals
class Dog extends Animal {
    // Override (override) method
    public void cry() {
        System.out.println("Wangwang");
    }

    public void eat() {
        System.out.println("I'm a dog. I love bones");
    }
}

// Cat class inherits from animal class
class Cat extends Animal {
    // Override (override) method
    public void cry() {
        System.out.println("Meow meow");
    }

    public void eat() {
        System.out.println("I'm a cat. I like fish");
    }
}

3. In addition, create food category:

// Food
class Food {

    String name;

    public String getName() {
        return name;
    }

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

    // There's a way to make it work in food
    public void showName() {

    }
}

4. Moreover, both cats and dogs have their own different favorite foods (they inherit from the food category):

// Fish (a kind of food) inherit from food
class Fish extends Food {
    public void showName() {
        System.out.println("Food: fish");
    }
}

// Bones (a kind of food) inherit from food
class Bone extends Food {
    public void showName() {
        System.out.println("Food: bone");
    }
}

5. Main human (to unify animals and corresponding food):

// There is a way of eating for the Lord
class Master {
    // Feed animals. If there is no polymorphism, he will write two ways to feed cats and dogs
    // With polymorphism, even if there are many more animals in the future, you can use this function
    public void feed(Animal an, Food f) {
        an.eat();
        f.showName();

    }
}

6. Finally, method call (test):

public class DuoTaiDemo {

    public static void main(String args[]) {

        Master master = new Master();
        master.feed(new Dog(), new Bone());

        // hin Convenient, you can try again
        master.feed(new Cat(), new Fish());

    }
}

Tags: Java Programming

Posted on Tue, 05 May 2020 10:14:52 -0400 by robcrozier