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.
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.
-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".
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);
Classes with even one ambiguous method must always have ʻabstract`.
-Because it cannot be created as an instance, it is prevented from being instantiated by mistake.
-Abstract methods using ʻabstract` for all methods
Access modifier interface Interface name (argument);
Access modifier class class name implements parent interface name{
}
implement = means to implement
Implementing an interface forces the implementation of the method and guarantees that at least the members who have that interface will have it.
-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
---------- (Troublesome example) --------------- ・ Human class "Move method" Walk and move
・ Devil class "Move method" Fly and move ↓ Multiple inheritance to generate a devilman!
・ Devilman class
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
"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. ** **
Access modifier class class name implements parent interface name 1,Parent interface name 2 ...{
}
Access modifier class class name extends parent class name implements parent interface name 1,Parent interface name 2 ...{
}
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