[JAVA] Polymorphism that even beginners of object-oriented programming can understand

What is polymorphism?

Wikipedia says:

Polymorphism describes the nature of the type system of a programming language, and that each element of the programming language (constants, variables, expressions, objects, functions, methods, etc.) belongs to multiple types. Refers to the property of forgiving. Also called polymorphism, polymorphism, polymorphism, diversity.

It's difficult to say that each element of a programming language belongs to multiple types.

In short, polymorphism is a common main routine.

What is a common main routine?

image.png

Why use polymorphism

Implementation of polymorphism


Door dr = new Door();     // OK:The types on the left and right sides match (Note: Door is a self-made class)
Door dr2 = new String();  // NG:Compile error because the types on the left and right sides do not match
ClassA a = new ClassB();     // OK
//ClassA is a superclass and ClassB is a subclass
public class ClassB extends ClassA {
    //abridgement
}

Specific examples of polymorphism

1. Arrangement using polymorphism

Caller (class with main method)

The variable players is declared as an "array of Player classes". An instance of another class (a subclass of the Player class) is assigned to the contents of the array.

This is polymorphism (common main routine). There is one way to call "players [X] .janken ()", but there are multiple methods to be called (Person's rock-paper-scissors, Robot's rock-paper-scissors).

MainClass.java


public class MainClass {

    public static void main(String[] args) {

        // (1)Generate a player to play rock-paper-scissors
        Player[] players = new Player[3];
        players[0] = new Person();  //Man
        players[1] = new Robot();   //Robot A
        players[2] = new Robot();   //Robot B

        // (2)Let each player play rock-paper-scissors in turn
        players[0].janken();    //Man
        players[1].janken();    //Robot A
        players[2].janken();    //Robot B
    }
}

Callee (superclass)

Player.java


public class Player {

    private int win;

    public void janken() {
        //Rock-paper-scissors processing
    }

    public int getWin() {
        return win;
    }
}

Callee (subclass)

Person.java


public class Person extends Player {
    //abridgement
}

Robot.java


public class Robot extends Player {
    //abridgement
}

In the future, even if the processing of the janken method changes, the caller does not have to change either (1) or (2). Also, even if the program is modified as shown below, the caller code only needs to be changed in (1).

2. Example of using polymorphism as an argument

Suppose there is a specification change in the rock-paper-scissors program. I added the code of "[Add]" to MainClass.java earlier.

MainClass.java


public class MainClass {

    public static void main(String[] args) {

        // (1)Generate a player to play rock-paper-scissors
        Player[] players = new Player[3];
        players[0] = new Person();  //Man
        players[1] = new Robot();   //Robot A
        players[2] = new Robot();   //Robot B

        //[Addition] Added processing to register for Janken Tournament
        Entry[] entry = new Entry[3];
        entry[0] = new Entry(players[0]);   //Man
        entry[1] = new Entry(players[1]);   //Robot A
        entry[2] = new Entry(players[2]);   //Robot B

        // (2)Let each player play rock-paper-scissors in turn
        players[0].janken();    //Man
        players[1].janken();    //Robot A
        players[2].janken();    //Robot B
    }
}

Entry.java


public class Entry {

    private int win;

    //constructor
    public Entry(Player player) {
        //Get player information (history of wins, etc.) to register for the Janken Tournament
        win = player.getWin();
    }
}

Summary

What is polymorphism?

Why use polymorphism?

References

If you want to learn more about object-oriented programming, I recommend you to read it. You can buy it on Amazon.

Recommended Posts

Polymorphism that even beginners of object-oriented programming can understand
"Inheritance" that even beginners of object-oriented programming can understand
Basics of jQuery that even freeters can understand
Explaining "object-oriented" that even elementary school students can understand in 5 levels
Scala's Implicit that even Java shops can understand
[JQuery] A guy that even beginners could understand well
What is object-oriented programming? ~ Beginners ~
Android environment construction that even monkeys can understand [React Native]
Summary of object-oriented programming using Java
JSP + Eclipse + Jetty development environment construction that even Java beginners can do
I tried learning Java with a series that beginners can understand clearly
Until programming beginners can use lambda expressions
Object-oriented that can be understood by fairies
Object-oriented programming
Awareness of object-oriented programming for half a year
[Book Review] Unit testing of programming sites that can be done with zero experience
Understand while reading the article! Summary of contents that Elasticsearch beginners want to suppress