C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
In main: We create instances of the Ruby and Java classes, but reference them through a Site variable.
Display: When we call the display() method the "override" implementations are located at runtime and invoked.
Java program that has interfaces
interface Site {
public void display();
}
class Ruby implements Site {
public void display() {
System.out.println("Ruby.display");
}
}
class Java implements Site {
public void display() {
System.out.println("Java.display");
}
}
public class Program {
public static void main(String[] args) {
// Use implementations through an interface.
Site site = new Ruby();
site.display();
Site site2 = new Java();
site2.display();
}
}
Output
Ruby.display
Java.display
Tip: With Collection, we can act upon many classes, like ArrayList and Vector, with unified code.
ArrayListVectorHere: A display() method receives an ArrayList and a Vector but provides only one implementation.
Info: The type parameter (such as Integer) must match for the interface code to compile.
Java program that uses Collection interface
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;
public class Program {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
Vector<Integer> vec = new Vector<>();
vec.add(2);
vec.add(3);
// Treat the collections by their interface.
display(list);
display(vec);
}
static void display(Collection<Integer> col) {
// Display size of collection.
System.out.println(col.size());
}
}
Output
1
2
Syntax: We separate the interface names with a comma. The "implements" keyword is not repeated.
Java program that uses multiple interfaces
interface Box {
public int area(boolean accurate);
}
interface Cube {
public int volume(boolean accurate);
}
class Unit implements Box, Cube {
public int area(boolean accurate) {
if (accurate) {
return 4;
} else {
return 1;
}
}
public int volume(boolean accurate) {
if (accurate) {
return 100;
} else {
return 10;
}
}
}
public class Program {
public static void main(String[] args) {
// Create class and call methods.
Unit unit = new Unit();
System.out.println(unit.area(true));
System.out.println(unit.volume(false));
}
}
Output
4
10
Here: We reference an ArrayList as a Collection. Then we cast the Collection interface variable back to an ArrayList.
Java program that casts from interface
import java.util.ArrayList;
import java.util.Collection;
public class Program {
public static void main(String[] args) {
// Use Collection interface with ArrayList.
Collection<String> coll = new ArrayList<>();
coll.add("cat");
// Cast Collection to ArrayList.
ArrayList<String> list = (ArrayList<String>) coll;
System.out.println(list.size());
}
}
Output
1
Java program that uses interface, method, throws
interface Coffee {
public void brew(int size) throws Exception;
}
class MediumRoast implements Coffee {
public void brew(int size) throws Exception {
// Throw an exception if size is too big.
if (size > 100) {
throw new Exception("Too much coffee");
}
}
}
public class Program {
public static void main(String[] args) throws Exception {
Coffee cup = new MediumRoast();
cup.brew(1000);
}
}
Output
Exception in thread "main" java.lang.Exception: Too much coffee
at program.MediumRoast.brew(Program.java:12)
at program.Program.main(Program.java:20)
Here: The CardboardBox and PlasticBox classes implement the Box interface. We add instances of them to the HashMap in main.
Get: We call get() and access interface references. We can use the interface without determining the actual class types.
Java program that uses HashMap and interface
import java.util.HashMap;
interface Box {
public void test();
}
class CardboardBox implements Box {
public void test() {
System.out.println("cardboard");
}
}
class PlasticBox implements Box {
public void test() {
System.out.println("plastic");
}
}
public class Program {
public static void main(String[] args) {
HashMap<String, Box> map = new HashMap<>();
// ... Put new class instances in HashMap.
map.put("ship-100", new CardboardBox());
map.put("pack-200", new PlasticBox());
// ... Get interface reference from the HashMap.
// Call test on it.
Box b = map.get("ship-100");
if (b != null) {
b.test();
}
}
}
Output
cardboard
Version 1: This version of the code calls a method through an interface reference (the interface is called Tester).
Version 2: Here we call a class method directly on the Perl class. No interface is used for this version of the code.
Result: The direct method call to test() is about three times faster than an interface call to the same method.
Java program that times interface method call
interface Tester {
public void test(int value) throws Exception;
}
class Perl implements Tester {
public void test(int value) throws Exception {
// Some useless logic.
if (value < 0) {
throw new Exception();
}
}
}
public class Program {
public static void main(String[] args) throws Exception {
Tester test = new Perl();
Perl perl = (Perl) test;
long t1 = System.currentTimeMillis();
// ... Version 1: call interface method.
for (int i = 0; i < 10000000; i++) {
test.test(i);
}
long t2 = System.currentTimeMillis();
// ... Version 2: call class method.
for (int i = 0; i < 10000000; i++) {
perl.test(i);
}
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
27 ms, Interface method call
8 ms, Non-interface method call