Inheritance is used to create another class that derives from one class. For example, if you already have a "hero" class and want to create a "superhero" class that derives from it.
class Class name extends The original class name{
Members to add to the original class name
}
(Hero class)
public class Hero {
//field
private String name;
private int hp;
}
If you want to add a flying superhero to this ...
(SuperHero class)
public class SuperHero extends Hero{
private boolean flying;
public void fly() {
this.flying = true;
System.out.println("I jumped up!");
}
public void land() {
this.flying = false;
System.out.println("Landed!");
}
}
"Original class" ・ ・ ・ Parent class, super class "Class created based on the original class" ... Child class, subclass
In the above code, the inheritance relationship is as follows. "Hero class" ・ ・ ・ Parent class, super class "Super Hero class" ・ ・ ・ Child class, subclass
-Multiple parent classes cannot be inherited (multiple inheritance) at the time of inheritance.
-Classes with final
when creating a class cannot be inherited.
Rewrite the members of the parent class with the child class. At this time, the members of the parent class are not rewritten.
If you want to rewrite the run method. (Parent class)
public class Hero {
//field
private String name;
private int hp;
public void run() {
System.out.println("ran away!");
}
(Child class)
public class SuperHero extends Hero{
private boolean flying;
public void run() {//Members to rewrite
System.out.println("Withdrew!");
}
}
(Main class)
public static void main(String[] args) {
//Hero
Hero hero1 = new Hero();
hero1.run();
//Superhero
SuperHero shHero1 = new SuperHero();
shHero1.run();
}
(Output result)
ran away!
Withdrew!
It can be prohibited by adding final
to the method that prohibits overriding.
If you want to prevent overriding the run method.
public final void run
The SuperHero
instance created above has a double structure of Hero instance
and SuperHero
instance.
(Image)
At the time of calling, it works so that it is used from the members of the SuperHero instance first.
(Image)
In this case, the run method
is in'SuperHero`, so it will be called.
And since the method was found in ①, the call in ② is not executed.
How to use members of a parent class in a child instance
Field ・ ・ ・ super. Field name
Method ・ ・ ・ super. Method name (argument)
In the SuperHero class
, the ʻattack method, if it is flying, it attacks twice. The content of the attack is when you want to use the ʻattack method
of the parent class Hero class
.
//Hero class
public void attack(Matango matango) {
int matangoHp = matango.getHp() - 5;
matango.setHp(matangoHp);
}
//SuperHero class
public void attack(Matango matango) {
super.attack(matango);
if(this.flying) {
super.attack(matango);
}
No access to the parent class (grandparent class) of the parent class.
Instances created by inheritance always call the parent constructor before calling their own constructor.
Child class name(){
super(argument);
}
However, if nothing is stated, ** implicit super ** will be entered automatically.
(Hero class)
Hero(){
System.out.println("Generated Hero");
}
(SuperHero class)
SuperHero(){
//Implicit super enters here
System.out.println("Generated Super Hero");
}
(main class)
SuperHero shHero1 = new SuperHero();
(Output result) SuperHero is not generated, but the SuperHero constructor is called.
Generated Hero
Generated Super Hero
If the parent class constructor requires an argument, you need to explicitly put a value in the argument of ** super (argument) **.
Child class is-a Parent class (child class is a kind of parent class) must be established.
○ Correct example: Child class (TV) -Parent class (home appliances) "TV is a home appliance" → Yes × Wrong example: Child class (TV) -Parent class (car) "TV is a car" → Does not hold
Recommended Posts