This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I'm somewhat confused about the how interfaces are "created". It seems that an object can "implement" and interface. (For example, when a class implements an ActionListener.) But I've been reading about collections, specifically LinkedList and ArrayList, and although it appears they are interfaces, they seem to be created like other objects are variables. (For example List myList = new list; ) Why is this? Why don't you create an object that "implements" a list like the example with the ActionListener? I know understanding this concept may not critical to writing Java code, but I am curious. Any explanation would be greatly appreciated. Thanks, Landon [ April 30, 2004: Message edited by: Landon Blake ] [ April 30, 2004: Message edited by: Landon Blake ]
LinkedList and ArrayList are not interfaces -- they're concrete classes. But both of these concrete classes are implementations of the java.util.List interface. That means you can write List myList = new LinkedList(); and then work with the variable myList using the various methods of the List interface. The beauty of this is that you might work on this program for a while and then find that you need to be able to sort myList. Sorting a LinkedList is a very slow operation; ArrayLists are much more appropriate. You could, then, modify that single line: List myList = new ArrayList(); and nothing else would have to change, but all of a sudden, the sorting performance would improve dramatically.
I know understanding this concept may not critical to writing Java code
Actually I think it is critical so it's good that you ask. As you stated, you cannot instantiate an interface, rather you instantiate an implementation of an interface. LinkedList and ArrayList are actually implementations of the List interface. If you look in the Java API's, you find them both listed in the "Classes" section, not the "Interfaces" section. I think you are more confused about the difference between what an object instance really "is" and how it is being "referenced". For eaxmple: In that code, you are creating an ArrayList (that's what it really is), but you are going to "refer" to it as a generic List type. That's possible because an ArrayList is an implementation of List. You see that pattern of code (referencing an object by it's interface or super class) a lot because it is considered good form in many cases because it hides implementation details. Typically a client (another object, another app, etc) does not need to know that a List is actually handled via an ArrayList or LinkedList, all it really cares about is that the object appears to be some type of List and therefore can do all of the things that a List can do (see the List methods).
Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Joined: Dec 04, 2003
Blake and Ernest, Thank you for your responses. I know understand this concept much better than I did before. I appreciate it. Landon [ April 30, 2004: Message edited by: Landon Blake ]