2021-10-6 Java learning (Interface)

Keywords: Java

1, Interface concept

A set of requirements for a class that you want to conform to this interface

Code of eg.Comparable interface

public interface Comparable
    int compareTo(Object other);

Any class that implements the Comparable interface must contain the compareTo method

  • All methods in the interface are automatically public methods. When declaring methods in the interface, you do not need to provide the keyword public. When implementing an interface, you must declare the method as public
  • The interface will never have an instance field, but it can contain constants. It is always public static final
  • Before Java 8, methods would never be implemented in interfaces
  • You can declare the interface variable Comparable x;
  • The interface variable must refer to the class object x=new Employee(...) that implements this interface
  • Instanceof can be used to check whether an object implements a specific interface obj instanceof Comparable
public interface Moveable {
    void move(double x,double y);
    double SPEED=95;
//Fields in the interface are always public static final

2, Class implementation interface


  1. Let the class be declared to implement the given interface implements
  2. Provide definitions for all methods in the interface

eg. if you want the sort method of the Arrays class to sort the employee object array, the Employee class must implement the Comparable interface
class Employee implements Comparable

  public int compareTo(Object o) {
        Employee other=(Employee) o;
        return Double.compare(salary,other.salary);
public class EmployeeInterface implements Comparable<EmployeeInterface>{
    private String name;
    private double salary;

    public EmployeeInterface(String name,double salary)

    public String getName() {
        return name;

    public double getSalary() {
        return salary;

    public void setSalary(double salary) {
        this.salary = salary;

    public void raiseSalary(double byPercent)

    public int compareTo(EmployeeInterface o) {
        return Double.compare(this.salary,o.salary);

import java.lang.reflect.Array;
import java.util.Arrays;

public class EmployeeSortTest {
    public static void main(String[] args) {
        EmployeeInterface[] staff=new EmployeeInterface[3];
        staff[0]=new EmployeeInterface("Harry Hacker",35000);
        staff[1]=new EmployeeInterface("Carl Cracker",75000);
        staff[2]=new EmployeeInterface("qaq",100);

        Arrays.sort(staff); //The sorting interface must be compatible

        for(EmployeeInterface e:staff)
            System.out.println("name="+e.getName()+" salary="+e.getSalary());

//name=qaq salary=100.0
//name=Harry Hacker salary=35000.0
//name=Carl Cracker salary=75000.0

3, Interface and abstract class

Abstract class problem: each class can only extend one class

Each class can implement multiple interfaces

C + + supports multiple inheritance, but Java does not

4, Default method

Use the default keyword to provide a default implementation for interface methods

    default int compareTo(T other)
        return 0;

eg. Iterator interface

Resolve default method conflicts:
First define a method as the default method in an interface, and then define the same method in a superclass or another interface. what happened?

  1. Super class priority
  2. Interface conflict

5, Design pattern: callback

In this mode, you can specify the action that should be taken when a specific event occurs.

Java: pass an object of a class to the timer, and then the timer calls the method of this object. Because objects can carry some additional information, passing an object is much more flexible than passing a function.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Instant;
import javax.swing.*;

public class TimerTest {
    public static void main(String[] args) {
        TimePrinter listener=new TimePrinter();//Create listener object
        Timer timer=new Timer(1000,listener); //The listener object is passed here

        JOptionPane.showMessageDialog(null,"Quit program?");


class TimePrinter implements ActionListener
    public void actionPerformed(ActionEvent event) {
        System.out.println("At the tone,the time is "+ Instant.ofEpochMilli(event.getWhen()));

Posted by maddali on Wed, 06 Oct 2021 06:06:10 -0700