Understand java interface in your own way

Introduction

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.

What is interface? ??

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 use 1] Creating an interface

How to write is as follows

Access modifier interface Interface name{}

[Usage 2] Interface implementation class

How to write is as follows

class class name implements interface name{
Processing content
}

I will actually write

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.

Why is it convenient? ??

Maintainability

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.

Prevents mounting omissions

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.

Can be inherited multiple times

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.

in conclusion

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.

Reference material

This article was written with reference to the following information.

-Oracle Certification Textbook Java Programmer Silver SE11

Recommended Posts

Understand java interface in your own way
Handle your own annotations in Java
Make your own simple server in Java and understand HTTP
Callable Interface in Java
Create your own Java annotations
How to read your own YAML file (*****. Yml) in Java
Access the network interface in Java
Make your own persistence FW (Java)
java (interface)
[java] interface
Java: Try implementing your own comma-separated formatter
How to create your own annotation in Java and get the value
Partization in Java
Java method call from RPG (method call in own class)
Changes in Java 11
Rock-paper-scissors in Java
Create your own Android app for Java learning
[Java] About interface
[Java] Understand in 10 minutes! Associative array and HashMap
[Java] Functional interface
About interface, java interface
Pi in Java
Understand java constructor
FizzBuzz in Java
Contemplation: How to take advantage of functional interfaces in your own functions (java)
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Java classes and instances to understand in the figure
Use of Abstract Class and Interface properly in Java
Call your own method with PreAuthorize in Spring Security
About Java functional interface
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
java standard functional interface
Comments in Java source
Azure functions in java
Understand Java 8 lambda expressions
Format XML in Java
Simple htmlspecialchars in Java
JAVA learning history interface
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
Make your own pomodoro
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java