The java interface often appears in the field, but I was working in ambiguity. Here, I will investigate and summarize it in my own way.
If you look up the meaning of the word on wikipedia "The method of passing information when communicating between software is decided."
In java, the specific processing content of the class is not described A description of only constants and methods.
It describes the specifications of the class when you chew it in your own way.
How to write is as follows
Access modifier interface Interface name{}
How to write is as follows
class class name implements interface name{
Processing content
}
Main.java
//Interface
interface Cat{
String NAME = "tama";
void helloName();
}
//Interface implementation class
class Sample implements Cat {
public void helloName() {
System.out.println("Hello," + NAME);
}
}
//Run
class Main {
public static void main(String[] args) {
Sample a = new Sample();
a.helloName();
}
}
Execution result
Hello, tama
I was able to execute it normally! In the interface implementation class Sample, the interface is specified by implements. This makes the constants of the interface class Cat available in Sample.
Also, the method defined in the interface class is judged as an abstract method without describing the qualifier. So, in the implementation class of the interface, the method (helloName method in this case) Must be overridden.
Since the implementation proceeds according to the format, it leads to a reduction in development time. Since the format is fixed, it is possible to prevent a lot of similar methods for each class.
Also, what I felt in the field is simple and easy to follow.
The method defined in the interface is an abstract method and needs to be overridden. Therefore, it is possible to prevent omission of method implementation.
Isn't it okay to use an abstract class instead of an interface while studying? I thought that the interface implements multiple interfaces It differs from abstract classes in that it can be done. You do not have to write the same description for each class.
This time I tried to summarize the java interface. Even if I understand how to write it, I can't understand it unless I understand why it's convenient and why it's necessary. I now realize that it is important to incorporate that into my own way.
This article was written with reference to the following information.
-Oracle Certification Textbook Java Programmer Silver SE11
Recommended Posts