Inheritance

Basics of inheritance

Way of thinking

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.

how to use

class Class name extends The original class name{
Members to add to the original class name
}

Example

(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!");
	}
}

How to express inheritance relationship

"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

Prohibitions of inheritance

-Multiple parent classes cannot be inherited (multiple inheritance) at the time of inheritance. -Classes with final when creating a class cannot be inherited.

override

Way of thinking

Rewrite the members of the parent class with the child class. At this time, the members of the parent class are not rewritten.

Example

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!

How to prohibit overriding

It can be prohibited by adding final to the method that prohibits overriding.

Example

If you want to prevent overriding the run method. public final void run

Class state at the time of inheritance

Way of thinking

The SuperHero instance created above has a double structure of Hero instance and SuperHero instance.

(Image) スクリーンショット 2018-02-26 10.37.12.png

Behavior at call

At the time of calling, it works so that it is used from the members of the SuperHero instance first.

(Image) スクリーンショット 2018-02-26 10.42.45.png

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 access the parent class

Way of thinking

How to use members of a parent class in a child instance

how to use

Field ・ ・ ・ super. Field name Method ・ ・ ・ super. Method name (argument)

Example

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);
		}

important point

No access to the parent class (grandparent class) of the parent class.

About inheritance and constructors

Way of thinking

Instances created by inheritance always call the parent constructor before calling their own constructor.

How to call the constructor of the parent class

Child class name(){
    super(argument);
}

However, if nothing is stated, ** implicit super ** will be entered automatically.

Example

(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

Supplement

If the parent class constructor requires an argument, you need to explicitly put a value in the argument of ** super (argument) **.

Precautions for inheritance

is-a relationship

Child class is-a Parent class (child class is a kind of parent class) must be established.

Example

○ 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

Inheritance
Inheritance
About inheritance
[Java] Inheritance
Java inheritance
Java inheritance
Advanced inheritance
java (inheritance)
[Java] Class inheritance
About class inheritance.
Ruby Learning # 33 Inheritance
About Ruby inheritance
About java inheritance
Inheritance and interface.
[Java] Overload / Override / Inheritance
Summarize Java inheritance (Java Silver 8)
Notes on multiple inheritance
About inheritance (Java Silver)
Ruby inheritance and delegation
About encapsulation and inheritance
[Java] Implicit inheritance memo
Java learning memo (inheritance)