[Java] About interface

I'm learning Java and would like to summarize one of the points (?) Interface that is easy to trip over.

An example of interface on Android is summarized in another article.

-[Android] interface example

If you want to deepen your understanding of interface in Android application programming, I think it's a good idea to create your own listener.

-Android Countdown

About interface

How to write an interface.

Runnable in java.lang is an example interface.

Runnable.java


package java.lang;


/**
 * Represents a command that can be executed. Often used to run code in a
 * different {@link Thread}.
 */
public interface Runnable {

    /**
     * Starts executing the active part of the class' code. This method is
     * called when a thread is started that has been created with a class which
     * implements {@code Runnable}.
     */
    public void run();
}

To make it simple

Runnable.java


public interface Runnable {
    public void run();
}

It will be like this.

A refreshing introduction to Java In the second edition, it is sometimes explained by the expression "abstract class in abstract class".

Two conditions treated as an interface

  1. All methods are abstract methods
  2. Basically it has no fields

Abstract method

Contents of processing (without {/ * ... processing ... * /}) Method.

Runnable.java


public void run();

Part of.

About interface fields

When you declare a field in the interface, it is treated as if you declared a constant in public static final.

Instance generation

Abstract methods must be ** always overridden **, so when instantiating ʻinterface`,

python


Runnable runnable = new Runnable() {
    @Override
    public void run() {

    }
};

In this way, you will create an instance while overriding the run () method.

reference

--Introduction to Java that you can understand clearly 2nd edition -Interface Runnable

Recommended Posts

About Java interface
[Java] About interface
About interface, java interface
About Java functional interface
java (interface)
[java] interface
[Java] About Java 12 features
[Java] About arrays
[Java beginner] About abstraction and interface
Something about java
Where about java
About Java features
About Java threads
About Java class
About Java arrays
[Java] Functional interface
About java inheritance
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
Java, about 2D arrays
About class division (Java)
Commentary: About the interface
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
Callable Interface in Java
java standard functional interface
[Java] About Singleton Class
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
JAVA learning history interface
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
Java learning memo (interface)
All about Java programming
About java abstract class
A note about Java GC
Advanced inheritance abstract, interface -java
What I researched about Java 8
About an instance of java
JAVA learning history interface inheritance
[Java] Functional interface / lambda expression
What I researched about Java 6
[Gradle] About Java plug-in tasks
interface
What I researched about Java 9
About Java class loader types
Check Java9 Interface private methods
[Java Silver] About equals method