1// interface in java example
2interface Vehicle
3{
4 public void accelerate();
5}
6class BMW implements Vehicle
7{
8 public void accelerate()
9 {
10 System.out.println("BMW accelerating...");
11 }
12}
13public class InterfaceDemo
14{
15 public static void main(String[] args)
16 {
17 BMW obj = new BMW();
18 obj.accelerate();
19 }
20}
1// Assume we have the simple interface:
2interface Appendable {
3 void append(string content);
4}
5// We can implement it like that:
6class SimplePrinter implements Appendable {
7 public void append(string content) {
8 System.out.println(content);
9 }
10}
11// ... and maybe like that:
12class FileWriter implements Appendable {
13 public void append(string content) {
14 // Appends content into a file
15 }
16}
17// Both classes are Appendable.
1/* File name : Animal.java */
2interface Animal {
3 public void eat();
4 public void travel();
5}