Advanced inheritance

The idea of abstract

For example, when creating a class called "sportsman" / "reader" from a class called "human" (eating, walking, sleeping), for each "sportsman" / "reader" class, the inheritance source of the human class The amount of (eat, sleep) is different. (Sportsmen eat more than readers, and readers stay up late and sleep less than sportsmen who are tired and sleep well.) In this case, the human class cannot define the details about (eating, sleeping). However, human beings always do (eat, walk, sleep) regardless of whether they are "sportsmen" or "readers". From an object-oriented perspective, what you do in a human class must be described in the human class. Function for that.

How to define an ambiguous method ①

In the above example, (eat, sleep) is a method whose details cannot be defined, so it can be defined as an incomplete method by adding ʻabstract` when creating the method.

//Method definition
Access modifier abstract Return value Method name (argument);

By declaring a method with ʻabstracrt`, you can define a method of an ambiguous class and obtain the following effect.

effect

-If you do not override and implement it, an error will occur, so prevent forgetting to override * It will not be instantiated as ambiguous ・ It is possible to distinguish between the definition that cannot be defined and the definition that "does nothing".

How to define an ambiguous class ②

Similarly, how to define a class with ambiguous methods, like the "human" class above, is as follows.

//Class definition
Access modifier abstract class Return value Class name (argument);

rule

Classes with even one ambiguous method must always have ʻabstract`.

effect

-Because it cannot be created as an instance, it is prevented from being instantiated by mistake.

About the interface

rule

-Abstract methods using ʻabstract` for all methods

How to make

Access modifier interface Interface name (argument);

Inheritance method

Access modifier class class name implements parent interface name{
}

implement = means to implement

Benefits of using the interface

Implementing an interface forces the implementation of the method and guarantees that at least the members who have that interface will have it.

Feature

-Interface methods are automatically marked with ** public abstract ** -Interface fields are always marked with ** public static final ** ・ Multiple inheritance is possible * Details are described below

About multiple inheritance

---------- (Troublesome example) --------------- ・ Human class "Move method" Walk and move

・ Devil class "Move method" Fly and move   ↓ Multiple inheritance to generate a devilman!

・ Devilman class hqdefault.jpg

Which of the "move methods" should be inherited ...? ?? ??   ――――――――――――――――――――――――――――――――――――――――――

---------- (For interfaces) --------------- ・ Human interface "Move method" not implemented ・ Devil interface "Move method" not implemented

↓ Multiple inheritance to generate a devilman!

・ Devilman class story_img_1.jpg

"Move method" implementation: Fly and move

――――――――――――――――――――――――――――――――――――――――――

In short, there is a risk of implementation conflicts as various classes may have methods like move. However, in the case of an interface, since it is not implemented, even if the method is covered, it is implemented in the child interface, so there is no conflict. Therefore, ** interface can be inherited multiple times. ** **

Multiple inheritance method

Access modifier class class name implements parent interface name 1,Parent interface name 2 ...{
}

When using extend implements at the same time

Access modifier class class name extends parent class name implements parent interface name 1,Parent interface name 2 ...{
}

Summary of how to use extend implements

Inheritance source Inheritance destination Words used for inheritance Number of inheritance sources
class class extend 1
interface class implement One or more
interface interface extend One or more

Recommended Posts

Advanced inheritance
Inheritance
Inheritance
Advanced inheritance abstract, interface -java
About inheritance
[Java] Inheritance
Java inheritance
Java inheritance
java (inheritance)
[Java] Class inheritance
About class inheritance.
Ruby Learning # 33 Inheritance
About Ruby inheritance
About java inheritance
Inheritance and interface.