How do I implement interfaces in Java?

To implement an interface, a java class must use implements keyword on its class definition. For example, class A implements interface B. The class definition of class A would look like this:

class A implements B {
}

A class can implements more than one interfaces. For example, class A can implements interface B and interface C:

class A implements B, C {
}

Classes that implements an interface must implements all methods declared in that interface.

package org.kodejava.basic;

public interface Language {

    String getBirthday();
    String getGreeting();

}

The following class is an English implementation of the Language interface.

package org.kodejava.basic;

public class English implements Language {
    public String getBirthday() {
        return "Happy Birthday";
    }

    public String getGreeting() {
        return "How are you?";
    }
}

The following class is an Indonesian implementation of the Language interface.

package org.kodejava.basic;

public class Indonesian implements Language {
    public String getBirthday() {
        return "Selamat Ulang Tahun";
    }

    public String getGreeting() {
        return "Apa kabar?";
    }
}

And here is a snippet that show the interface and classes in action.

package org.kodejava.basic;

public class LanguageDemo {
    public static void main(String[] args) {
        Language language = new English();
        System.out.println(language.getBirthday());
        System.out.println(language.getGreeting());

        language = new Indonesian();
        System.out.println(language.getBirthday());
        System.out.println(language.getGreeting());
    }
}

How do I know if a class object is an interface or a class?

package org.kodejava.lang;

public class CheckForInterface {
    public static void main(String[] args) {
        // Checking whether Cloneable is an interface or class
        Class<?> clazz = Cloneable.class;
        boolean isInterface = clazz.isInterface();
        System.out.println(clazz.getName() + " is an interface = " + isInterface);

        // Checking whether String is an interface or class
        clazz = String.class;
        isInterface = clazz.isInterface();
        System.out.println(clazz.getName() + " is an interface = " + isInterface);
    }
}
java.lang.Cloneable is an interface = true
java.lang.String is an interface = false

How do I get interfaces implemented by a class?

The getClass().getInterfaces() method return an array of Class that represents the interfaces implemented by an object.

package org.kodejava.lang;

import java.util.Date;
import java.util.Calendar;

public class ClassInterfaces {
    public static void main(String[] args) {
        // Get an instance of Date class
        Date date = Calendar.getInstance().getTime();

        // Get all interfaces implemented by the java.util.Date class and
        // print their names.
        Class<?>[] interfaces = date.getClass().getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", date.getClass().getName(), i.getName());
        }

        // For the primitive type the interface will be an empty array.
        interfaces = char.class.getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", char.class.getName(), i.getName());
        }
    }
}

The java.util.Date class implements the following interfaces:

Interface of java.util.Date = java.io.Serializable
Interface of java.util.Date = java.lang.Cloneable
Interface of java.util.Date = java.lang.Comparable