Reasons to use String instead of StringBuffer:
1. Many methods that take a String argument don't accept a StringBuffer argument.
2. The String class has many useful methods and constructors not available from StringBuffer.
3. String is handled very efficiently in terms of memory and execution time by most jvm's.
Of course, objects that change often should be of class StringBuffer because every attempted modification to a String object actually creates a new object and modifies the String reference to point to the new object (less efficient than StringBuffer). If there are no other references to the old String object, it may be garbage collected. If there are other reference to the old String object, they still point to the old object, a source of confusion.
As for comparing abstract classes to interfaces, an abstract class can do everything an interface can do and much more, such as providing a concrete (fully coded) method to its children. However, a
Java child class can only inherit from one parent class (and the parent's parent, etc.) but a Java class can implement many interfaces.
The most common use of interfaces is as the type of a method parameter where the method accepts any object that implements specific methods, you don't care how. Think about a sorting method that can handle any set of objects, perhaps of different types, provided the objects all have a compareTo(Object o) method. Just specify that your sorting method accepts objects of type Comparable. The Comparable interface includes the signature of the compareTo(Object o) method, so the Java compiler will enforce your rule. Notice that each object can have a different parent class as long as it implements Comparable.
Another use of interfaces is a marker interface such as Serializable. A marker interface doesn't have any methods. It's just a sort of compiler-readable specification that objects implementing that interface have some capability or characteristic. You can use the interface name as a parameter type or just
test for it with the
instanceof operator.
[ July 05, 2004: Message edited by: Mike Gershman ]