[JAVA] Polymorphism

Read p491 through p592 of a refreshing introduction to Java and write about polymorphism that you are particularly convinced of.

What is polymorphism?

Roughly speaking, polymorphism is a concept that allows you to treat multiple similar things that are strictly different as the same thing. In the reference book, it was likened to a bill (banknote). If you give a 10,000-yen bill to a primitive man, you will think of it as a piece of paper with a picture on it. I don't think it can be used for anything, even if I enjoy watching it. I am happy when I give a 10,000 yen bill to a human. Because I know that the paper is a banknote and can be exchanged for something. It is like this. In short, "the same thing can be understood differently" and "the usage method changes depending on what you think of it". Expressed in terms of inheritance, is it like paper ← paper with pictures ← banknotes? Paper has a method of wiping off water. It has a method of adding and enjoying the paper on which the picture is drawn (those who cannot enjoy seeing the picture will not be considered this time. Everyone will enjoy seeing the picture). And the former two methods + a method that can be exchanged for things will be added. The important thing is that banknotes also have "wipe water" and "enjoy watching" methods. The usage increases depending on what you think of the banknote. It seems that we need to specify what the computer should think of as paper.

Benefits of polymorphism

I will explain in specific situations where the concept of polymorphism is useful, using the code in the reference book.

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 ) {
		//Recovers 50 HP
		ch.addHp(50);	
	}
  }
}

This reference book contains the code for setting that you are making an RPG. A party of five people with different occupations stays at the inn and recovers 50 HP for all of them. It is assumed that all occupation classes (Hero, Thief, Wizard) inherit the Character type. Here, all the instances of the class of each occupation are stored in the array called Character type c of the superclass. Then, the element of c is turned by the for statement, and 50 is added to HP by the method called addHp that the occupation class has. The ability to store a subclass type instance in this superclass type variable is a characteristic and merit of polymorphism. Strictly different classes, but can be assigned. If this is not possible, you will have to write one line at a time to restore 50 HP for each instance of each profession class. It's hard when there are 1000 instances. In this way, it is polymorphic that programmers can enjoy themselves by treating similar things as roughly the same thing.

Summary

The property that something different from polymorphism can be roughly regarded as the same thing. Conversely, the same thing can be treated as different with a slight difference. Due to this property, programmers can enjoy various benefits.

Recommended Posts

Polymorphism
Polymorphism
Polymorphism quiz
[Java] Polymorphism
About polymorphism
java (polymorphism)
Encapsulation, polymorphism
About Java Polymorphism super ()
java (merits of polymorphism)