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.
1interface methods{
2 public static hey();
3}
4
5class scratch implements methods{
6 // Required to implement all methods declared in an interface
7 // Or else the class becomes abstract
8 public static hey(){
9 System.out.println("Hey");
10 }
11}
1/* File name : MammalInt.java */
2public class MammalInt implements Animal {
3
4 public void eat() {
5 System.out.println("Mammal eats");
6 }
7
8 public void travel() {
9 System.out.println("Mammal travels");
10 }
11
12 public int noOfLegs() {
13 return 0;
14 }
15
16 public static void main(String args[]) {
17 MammalInt m = new MammalInt();
18 m.eat();
19 m.travel();
20 }
21}
1// interface syntax
2interface InterfaceName
3{
4 fields // by default interface fields are public, static final
5 methods // by default interface methods are abstract, public
6}