Java interfaces are abstract classes that can be used in other classes. Classes that implements an interfaces must implement all the methods that are defined in the interface class.
ExampleI wrote an interface named: ComputerInterface , this class is public and has 2 empty methods called "startUp" and "shutDown"
GeSHi (java):
public interface ComputerInterface {
void startUp();
void shutDown();
}
Created by GeSHI 1.0.7.20
If we want to implement the interface in a class we need to write another class that does that, so here we go. As you can see below this is a public class that implements the ComputerInterface. This means that the ComputerInterfaceDemo class must have atleast the methods that are defined in the ComputerInterface class as you can see below or the class will complain that you haven't implemented the methods.
GeSHi (java):
public class ComputerInterfaceDemo implements ComputerInterface {
public void startUp() {
System.
out.
println("Start up");
}
public void shutDown() {
System.
out.
println("Shut down");
}
}
Created by GeSHI 1.0.7.20