Created: 2023-09-03 18:42
Status: #concept
Subject: Programming
Tags: Java Java Class Inheritance Polymorphism Java Abstract Class java.util.List java.util.Map java.util.Set java.util.Collection

Java Interface

We can create an interface to define Methods that are inherited when a class implements SomeInterface.

They serve as "contracts" to be honored when a class Thing implements AnInterface. If those methods are not implemented in the class, the program will not function.

  • they require the method name, return type, and parameters, EXCLUDING the actual implementation.
  • Implementations of methods defined in the interface must always have public as their visibility attribute Java Class access modifier.
  • they reduce the need to depend on a parent class by ensuring a contract between the object API and rest of the program is followed.

Common Interfaces

  1. java.util.Stream allows us to process List objects with Lambda Expressions.
    • .map(i -> o), .reduce(), .sorted(), .filter(), .toArray(), .collect()
  2. java.util.Comparable defines the compareTo(T obj): int to establish a "natural order" of elements, similar to C's strcmp() behavior.

Syntax

We **can inherit multiple interfaces **by delimiting them with ,.

  • we can also extends a superclass, but it must go first before using implements.

public interface Readable {
    String read();
}
public class TextMessage implements Readable {
    private String sender;
    private String content;

    public TextMessage(String sender, String content) {
        this.sender = sender;
        this.content = content;
    }

    public String getSender() {
        return this.sender;
    }

    // the method we implemented from Readable
    public String read() {
        return this.content;
    }
}

Objects can be instantiated from interface-implementing classes just like with normal classes. They're also used in the same way, for instance, as an ArrayList's type.

TextMessage message = new TextMessage("ope", "It's going great!");
System.out.println(message.read());

ArrayList<TextMessage> textMessage = new ArrayList<>();
textMessage.add(new TextMessage("private number", "I hid the body.");

Interfaces as Variable Types

Since a subclass implements SomeInterface, we can declare variables with SomeInterface as its type and the compatible values are any subclass that implements it.

TextMessage message = new TextMessage("ope", "Something cool's about to happen");
Readable readable = new TextMessage("ope", "The text message is Readable!");
ArrayList<Readable> readingList = new ArrayList<>();

readingList.add(new TextMessage("ope", "never been programming before..."));
readingList.add(new TextMessage("ope", "gonna love it i think!"));
readingList.add(new TextMessage("ope", "give me something more challenging! :)"));
readingList.add(new TextMessage("ope", "you think i can do it?"));
readingList.add(new TextMessage("ope", "up here we send several messages each day"));


ArrayList<String> pages = new ArrayList<>();
pages.add("A method can call itself.");

readingList.add(new Ebook("Introduction to Recursion.", pages));

for (Readable readable: readingList) {
    System.out.println(readable.read());
}

Interfaces as Parameters

Since they can be used as Variables, they can also be used as Method parameters.

public class Printer {
    public void print(Readable readable) {
        System.out.println(readable.read());
    }
}

References