ERROR ON PREV
Interfaces
  1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

Interfaces

Here is some code from the lesson on strings.

String1.java
1public class String1 {
2    static void printChars(String s) {
3        System.out.println("String: "+s);
4        for(int i=0;i<s.length();i++) {
5            System.out.println("["+i+"] "+s.charAt(i));
6        }
7    }
8    public static void main(String[] args) {
9        for(int i=0;i<args.length;i++) {
10            printChars(args[i]);
11        }
12    }
13}

We created a method called printChars() that listed all the characters in a String. Later we introduced the StringBuffer, which was very similar to the String but was mutable. Is it possible to modify this method to work with either String or StringBuffer? The answer is yes:

Interf1.java
1public class Interf1 {
2    static void printChars(CharSequence s) {
3        System.out.println("CharSeq: "+s);
4        for(int i=0;i<s.length();i++) {
5            System.out.println("["+i+"] "+s.charAt(i));
6        }
7    }
8    public static void main(String[] args) {
9        String s = "Hello";
10        StringBuffer sb = new StringBuffer();
11        sb.append("Wor");
12        sb.append("ld");
13        printChars(s);
14        printChars(sb);
15    }
16}
$ javac Interf1.java
$ java Interf1
CharSeq: Hello
[0] H
[1] e
[2] l
[3] l
[4] o
CharSeq: World
[0] W
[1] o
[2] r
[3] l
[4] d
$ javap java.lang.CharSequence
Compiled from "CharSequence.java"
public interface java.lang.CharSequence{
    public abstract int length();
    public abstract char charAt(int);
    public abstract java.lang.CharSequence subSequence(int, int);
    public abstract java.lang.String toString();
}

CharSequence is something called an interface. An interface is like an abstract class, but it has no implemented methods. Both String and StringBuffer implement the CharSeqence interface.

While you can only extend a single class, you can implement many interfaces.

interf/A.java
1package interf;
2 
3public interface A {
4    int getA();
5}
$ javac interf/A.java
interf/B.java
1package interf;
2 
3public interface B {
4    int getB();
5}
$ javac interf/B.java
Interf2.java
1import interf.*;
2 
3public class Interf2 implements AB {
4    int ab;
5    public int getA() { return a; }
6    public int getB() { return b; }
7    public Interf2(int a,int b) {
8        this.a = a;
9        this.b = b;
10    }
11    public static void main(String[] args) {
12        Interf2 i = new Interf2(2,3);
13        System.out.println("a="+i.getA()+", b="+i.getB());
14    }
15}
$ javac Interf2.java
$ java Interf2
a=2, b=3