An interface defines a new type of data by specifying a set of method that any implementing class must provide. Unlike a class, an interface can not include method implementationsāthese are left to the implementing classes.
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class InterfaceExample {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<String>();
List<String> linkedList = new LinkedList<String>();
arrayList.add("Apple");
linkedList.add("Banana");
System.out.println(arrayList.get(0)); // Apple
System.out.println(linkedList.get(0)); // Banana
}
}
In this case both ArrayList and LinkedList are both implementations of the interface List, they both implement the same methods and conform to the same specification.