This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
There is a mechanism called ** interface ** that is used in a similar way to the ** abstract class ** we dealt with last time. An abstract class was a type of class, and interfaces are created for similar purposes, but their declaration and implementation methods are different.
declareInterface.java
interface Felidae
{
String type = cats;
void meetsCats();
}
The usual class
declaration is gone and is replaced by ʻinterface. Like a class, it can have fields and methods (** it can't have a constructor **), ** Objects such as
Felidae cat = new Felidae ();cannot be created **. Also, since
void meets Cats ()is an abstract method, it is necessary to ** prepare a concrete method ** by the class that implements the
Felidae` interface (override).
String type = cats;
is an interface field, but this is a ** constant ** and cannot be changed. That's because the fields in the interface are in the same state as with public static final
(I don't yet know why).
inplementedFlidae.java
class Housecat inplements Felidae
{
void meetsCats()
{
System.out.println("I found a cat. I think it's a domestic cat somewhere.");
}
}
Instead of the extension ʻextends, it is ʻimplements
, but the shape is the same as before.
Why do we need an abstract method interface? I'd like to do everything with class extension, but Java ** doesn't allow multiple inheritance by multiple classes **. The parent superclass is limited to only one. It seems that you should not think in the same way as the system diagram that humans think.
I've seen a few reasons for this, but it seems that there is some depth here, such as the call when the method name is worn and the diamond problem. Another language, C ++
, allows this multiple inheritance.
However, it is difficult to use what you have prepared once. Therefore, instead of multiple inheritance by classes, we are trying to maintain simplicity as a language by allowing only partial multiple inheritance by abstract classes and interfaces. ** Interface implementation can be done with two or more **. You can also ** extend the interface itself ** and ʻextends` superinterfaces to subinterfaces.
Looking at the part where inheritance by extension is allowed, I feel that Java is a language that is good at deductive development. Knowing the class interface that has already been created and assuming that you will build a new class while inheriting its functions means that you will start studying Java by first knowing the functions provided by the standard library.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)
Recommended Posts