I\'ve used Java for some time and I keep hearing about interfaces such as Cloneable
, Iterable
and other X-able
s.
I was wondering
Runnable is the one I use most.
The Interfaces you're most likely to implement are:
java.lang.Comparable
java.lang.Runnable
java.io.Serializable
Interfaces that you're most likely to call methods on but not implement yourself are:
java.lang.Appendable
(StringBuffer / StringBuilder / Writers)
java.lang.CharSequence
(String / StringBuffer / StringBuilder)
java.lang.Iterable
(Collections, either explicitly or with for Blah blah : List<Blah>
)
java.lang.Readable
(Readers)
java.io.Closeable
(Streams)
java.io.Flushable
(Streams)
java.util.Collection
(Collections)
java.util.Deque
(Collections)
java.util.List
(Collections)
java.util.Map
(Collections)
java.util.Set
(Collections)
Interfaces that are most likely to blow up in your face:
java.lang.Cloneable
Edit: Whoops, Throwable's not an interface.
Usually, it's better to write a Copy Constructor rather than use a clone()
method.
Here's a list of all of the *able classes and interfaces in java. There's a only a few that are really widely used: I'd add Comparable
and Runnable
to your list.
$ jar tf $JAVA_HOME/jre/lib/rt.jar | grep [a-z]able\.class | grep -v ^com | grep -v ^sun | sort
java/awt/Adjustable.class
java/awt/Container$WakingRunnable.class
java/awt/datatransfer/Transferable.class
java/awt/Dialog$WakingRunnable.class
java/awt/ItemSelectable.class
java/awt/print/Pageable.class
java/awt/print/Printable.class
java/awt/ScrollPaneAdjustable.class
java/io/Closeable.class
java/io/Externalizable.class
java/io/Flushable.class
java/io/Serializable.class
java/lang/Appendable.class
java/lang/Cloneable.class
java/lang/Comparable.class
java/lang/Iterable.class
java/lang/ProcessEnvironment$Variable.class
java/lang/Readable.class
java/lang/reflect/TypeVariable.class
java/lang/Runnable.class
java/lang/Throwable.class
java/rmi/activation/Activatable.class
java/util/Collections$SelfComparable.class
java/util/concurrent/Callable.class
java/util/concurrent/Executors$PrivilegedCallable.class
java/util/Formattable.class
java/util/Hashtable.class
java/util/Observable.class
javax/accessibility/AccessibleStreamable.class
javax/lang/model/type/TypeVariable.class
javax/management/remote/JMXAddressable.class
javax/naming/Referenceable.class
javax/script/Compilable.class
javax/script/Invocable.class
javax/security/auth/Destroyable.class
javax/security/auth/Refreshable.class
javax/sql/rowset/Joinable.class
javax/swing/JSlider$1SmartHashtable.class
javax/swing/JTable$ThreadSafePrintable.class
javax/swing/plaf/basic/BasicFileChooserUI$FileTransferHandler$FileTransferable.class
javax/swing/plaf/basic/BasicTextUI$TextTransferHandler$TextTransferable.class
javax/swing/plaf/basic/BasicTransferable.class
javax/swing/RepaintManager$DisplayChangedRunnable.class
javax/swing/Scrollable.class
javax/swing/SwingWorker$DoSubmitAccumulativeRunnable.class
javax/swing/TablePrintable.class
javax/swing/text/DefaultStyledDocument$ChangeUpdateRunnable.class
javax/swing/TransferHandler$PropertyTransferable.class
javax/swing/undo/StateEditable.class
org/omg/CORBA/portable/Streamable.class
There's a list of all the interfaces in the Java library's javadocs - follow the tree link then search for the "Interface Hierarchy" section.
You're correct re. Cloneable
and to partly answer your question, I would never use it. For more info read this interview with Joshua Bloch.
There are lots of interfaces, just as there are lots of classes, enums, and exceptions. If you just look at interfaces in isolation you will not see the complete picture. Some interfaces are nouns made into adjectives (-able) others are not - and the division is as much about what's sensible in English than any technical distinction.
It's probably best to investigate in the area you are trying to solve rather than looking into what interfaces are available across the JRE - most won't make much sense until you have a specific problem scenario in mind, and look at them in context with their collaborators.
As you are starting out, begin with interfaces in the java.lang
package, then java.io
, java.util
, and possibly java.util.concurrent
, this will give you a good grounding, and then look into specific application areas.
Good luck!