Use the concept of inheritance ʻis-ato make the outline of the instance recognized programmatically. For example, a way to programmatically recognize a
truck instance generated from the
truckclass as an instance of the
car` class.
In the case of automobiles and trucks ...
Car car =new Truck
①new Truck Create an instance.
②Car car What do you consider the created instance to be? This time, the instance was created from the "truck class", but it means that it is regarded as a "car".
①
②
If you assign an instance created from a child class to a parent class, the members of the child class cannot be used.
Due to the concept of inheritance, the method of the child class is also called when the method of the parent class is called.
-There are two types of the above figure: Wizard instance
, which is regarded as Wizard
, and Wizard instance
, which is regarded as Character
.
-Each method run
is called, but if the method run
is called in the Wizard instance
which is regarded as Character
, I wonder if the method of the Character
class will be called ...
-Since the method of ** child class ** is called preferentially based on the concept of inheritance, the instance of Wizard
class is called.
Replacing with the above example, the method when you want to change the wizard
instance, which is regarded as Character
, to the wizard
instance, which is regarded as Wizard
.
** Thinking normally ... **
Wizard wizard2 = character;
It looks good, but I get an error with this ...
The reason is that the wizard
instance is regarded as Character
, and the wizard
instance has been generally regarded as Character
. Therefore, from the computer's point of view, "Is this really awizard
?? There are other Character
s, right ??"
for that reason
Wizard wizard2 = (Wizard)character;
You must use the cast operator to force the class to change. In the above, it is forcibly changed to a (Wizard) type variable.
However
Character character = new Hero;//Think of a hero as a character!
Wizard wizard2 = (Wizard)character;//Think of the hero as a witch! (??)
The above mistakes may occur.
-Create a hero instance that is first captured as a character.
-Create a hero instance that can be seen as a wizard.
In this case, ** Is-a relationship does not hold **, so you will get the error ClassCastException
(forced change by cast is wrong).
・ ○ A hero is a character.
・ × Hero is a witch.
In other words, the only classes that can be forcibly changed by casting are the class used when the instance was created or the class after its child class **.
To determine if the forced conversion by cast is correct, java uses the ʻinstanceof operator. The ʻinstanceof
operator determines if there is a contradiction when assigning to the type specified by the cast operator.
if(character instanceof Wizard){//if,`character`To`Wizard`If you can consider it as
Wizard wizard2 = (Wizard)character;
}
By using an array for class variables when creating an instance, it is possible to perform batch processing for the instance.
** If you don't use an array **
If you want to generate Hero1``Hero2``Hero3
from the hero class and Wizard1``Wizard2``Wizard3
from the wizard class and let everyone attack
//Instance generation
Hero hero1 = new Hero();
Hero hero2 = new Hero();
Hero hero3 = new Hero();
Wizard wizard1 = new Wizard();
Wizard wizard2 = new Wizard();
Wizard wizard3 = new Wizard();
//Method call
hero1.attack();
hero2.attack();
hero3.attack();
wizard1.attack();
wizard1.attack();
wizard1.attack();
In this case, the method call is repeated in the same way, and it's not good enough ...
** With arrays **
//Creating an array
Character[] character = new Character[6];
//Instance generation
Character character[0] = new Hero;
Character character[1] = new Hero;
Character character[2] = new Hero;
Character character[3] = new Wizard;
Character character[4] = new Wizard;
Character character[5] = new Wizard;
//Method call
for(Character ch : character){
ch.attack();
}
The process of the method is neatly organized and feels good!
** If you want to attack a monster **
public static void main(String[] args) {
//Creating array variables
Monster[] monster = new Monster[4];
//Generating a monster instance
monster[0] = new Slime();
monster[1] = new Slime();
monster[2] = new Goblin();
monster[3] = new Goblin();
//Generating a hero instance
Hero hero = new Hero();
//Attack monsters
hero.attack(monster[0]);
}
public class Hero {
void attack(Monster monster) {
monster.hp-=10;
}
}
By doing this, it is not necessary to prepare the argument of the attack method
of the Hero
class for each type of monster.
Even if you call a method with the class name of the parent class, the method of the child class is called, so you can combine the method calls.
Slime class
public class Slime extends Monster {
public void run() {
System.out.println("The slime swelled and ran away!");
}
}
Goblin class
public class Goblin extends Monster{
public void run() {
System.out.println("The goblins ran away slapstick!");
}
}
Monster class
public abstract class Monster {
String name;
int hp = 50;
public abstract void run();
}
Main method
//Generating a monster instance
monster[0] = new Slime();
monster[1] = new Goblin();
//Call method in monster class
for(Monster mo:monster) {
mo.run();
Output result
The slime swelled and ran away!
The goblins ran away slapstick!
Although it is called by the monster class, the method contents of each class are executed. At this time, it is necessary to define a method to be called to the parent method with ʻabstrack`.