1

I’m hoping to get some clarification on a topic which I find very confusing: The relationship between ArrayList / List / ObservableList, which also ties into the differences between Classes and Interfaces. I’ve read multiple texts on the subject, and the more I read, the more confused I become.

Here’s what I do know for certain: ArrayList is a Class, which means you can create as many ArrayList objects as you like. (This is good news for me, because I love working with them.) I’m also learning JavaFX, and there’s a lot of FX objects which take ObservableLists as input. But ObservableLists are Interfaces, not Classes, so forget about creating any ObservableList objects. At first, I assumed that ArrayList was a child class of List, and ObservableList is an Interface to List & any List child Classes. …But reading further, I see that List is actually an Interface, not a Class, and the parent Class of ArrayList is AbstractList. So that didn’t clear up a lot for me.

Another bit that is driving me crazy: In another forum post, a wiser coder gave me this snippet of code to help my ArrayLists accessible via the ObservableList interface:

List<myClass> myList = new ArrayList<myClass>();

While this method of declaring and initializing an ArrayList compiles and works great, I just plumb don’t understand what is going on here. I thought List was an Interface, not a Class, so how can I create a myList object of type List? If I had to guess, I’d suppose that there is an Interface named List and another, separate Class named List, and due to the context here, Java assumes I am referring to the Class.

On a side question, is there a recommended website or text that people could recommend I consult on this topic? I’ve read “Java in a Nutshell,” the Oracle website, different posts on this site, “Java for Dummies,” and “Java in a Nutshell Examples,” but no illumination yet. Any guidance on understanding this topic is wildly appreciated.

Many thanks! -RAO

Pete
  • 1,511
  • 2
  • 26
  • 49
  • Sorry, I searched the forum before posting and didn't see what I thought was a duplicate post. – Pete Apr 21 '16 at 21:09

2 Answers2

1

Brief (so brief) resume:

java.util.List is an Interface. That is, defines the contract that all clases implementing that interface should code (except abstract classes) java.util.ArrayList is a class that implements that interface

As you can't instantiate any interface, you must use the implementing classes (f.i. ArrayList) but, you can manage that as an interface and no matter what is the real class behind it. You just know that your class implements all methods in the interface.

So,

List<myClass> myList = new ArrayList<myClass>();

Just means that you're instantiating a List (actually an ArrayList) and that means that you're going to manage that list through the methods defined in the java.util.List interface

Check this link for complete information about working with interfaces

Jorge
  • 1,136
  • 9
  • 15
  • Hi Jorge, A light bulb went off for me when I read this. Your answer made me think of an interface of type List sitting on top of an ArrayList object, like a sort of mask. I think I get it now - knowing an Inteface "fits" on top of an Object/Class allows you to work with that object. Aha! So awesome, thank you!!! – Pete Apr 21 '16 at 21:06
1

If several books and articles haven't helped you, maybe a different perspective will. Let's take a look at the statement you've outlined:

List<myClass> myList = new ArrayList<myClass>();

You're not actually instantiating a List here. You're instantiating an ArrayList, and referencing that object with a reference of type List. This means that when you're working with myList, the compiler will treat the object as a List even though the Object it's referencing is an ArrayList. This line compiles because the compiler looks to the instantiation versus the reference type and says, "Yes, this ArrayList is a List, and so this is legal.(Doing this allows you to ensure type safety by always havingmyListconform to typeList, no matter what kind ofList` it references).

An ArrayList is a List because it implements the List interface. However, interfaces don't define their methods. So what happens when we do this?

myList.add(myObj);

Here's a simplified version of how it happens. The compiler looks at myList.add and says "Yes, myList is a List, and List has a method add, and so this code works." Well, List doesn't define how it works (the method is essentially abstract), so when this action is actually run, it looks to the type of list the object is, ArrayList, which tells the machine to add the object to the end of the list. And so it does. (This is also why you must define every method of an interface when implementing it.)

I can't say this story-form answer is completely accurate but it might help things click for you.

Zircon
  • 4,677
  • 15
  • 32