java (merits of polymorphism)

Combination of polymorphism and sequence

Code that does not combine arrays

public class Main {
  public static void main(String[] args) {
    Hero h1 = new Hero();
    Hero h2 = new Hero();
    Thief t1 = new Thief();
    Wizard w1 = new Wizard();
    Wizard w2 = new Wizard();
    //Adventure begins!
    //First stay at the inn
    h1.hp += 50;
    h2.hp += 50;
    t1.hp += 50;
    w1.hp += 50;
    w2.hp += 50;

Code that combines arrays

public class Main {
  public static void main(String[] args) {
    Character[] c = new Character[5];
    c[0] = new Hero(); 
    c[1] = new Hero(); 
    c[2] = new Thief(); 
    c[3] = new Wizard(); 
    c[4] = new Wizard(); 
    //Stay in an inn
    for (Character ch : c) {      //Take out one by one
      ch.hp += 50;                //Recovers 50 HP

Combination with arguments

public class Hero extends Character{
  public void attack(Monster m) {               //Attack all monsters
    System.out.println( + "Attack!");
    System.out.println("10 points of damage to the enemy");
    m.hp -= 10;

Attack everything if you inherit the monster class

Make different actions with the same instructions

public class Main {
  public static void main(String[] args) {
    Monster[] monsters = new Monster[3];
    monsters[0] = new Slime();
    monsters[1] = new Goblin();
    monsters[2] = new DeathBat();
    for (Monster m : monsters) {;                    //Repeat the same instructions

Execution result Slime ran away Goblins ran away Hell bats escaped

Calling side, run away, run away, run away The called side slime ran away Goblins ran away Hell bats ran away

Recommended Posts

java (merits of polymorphism)
[Java] Polymorphism
java (polymorphism)
[Java] Overview of Java
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
java --Unification of comments
About Java Polymorphism super ()
History of Java annotation
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
About an instance of java
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
[Java] Practice of exception handling [Exception]
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
[Java] Creation of original annotation
4th day of java learning
[Java] Beginner's understanding of Servlet-①
Java end of month plusMonths
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
Enumeration of all combinations Java
java (inheritance of is-a principle)
Implementation of gzip in java
Advantages and disadvantages of Java
Benefits of Java static method
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java] I thought about the merits and uses of "interface"
[Java] Speed comparison of string concatenation
[Java] Delete the elements of List
[For beginners] Summary of java constructor
Various methods of Java String class
Root cause of java framework bugs
[Java version] The story of serialization
Summary of [Java silver study] package
About Lambda, Stream, LocalDate of Java8
Story of passing Java Gold SE8
Sort a List of Java objects
[Java] Output by FormatStyle of DateTimeFormatter
[Java] Input to stdin of Process
Basic usage of java Optional Part 1
[Java EE] Common misunderstanding of @Dependent
[Note] Handling of Java decimal point
[Java] Runtime Data Areas of JVM