• Post Reply Bookmark Topic Watch Topic
  • New Topic

Semantics of Java Collections Framework.  RSS feed

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning Everyone.

I'm having some difficulties with the semantics of the Java Collections Framework.

I think I understand the range of operations that can be performed on various collection types.

But what I don't get, is the meaning of this code:



The issue that bugs me and prevents me from moving on is this:

* List is an interface, and ArrayList is a class. So how can it be possible to assign an object of the ArrayList class to 'list' whose type is interface?

None of the online tutorials address this, am I missing something really obvious?

Any help, most welcome as always.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohammed, List is an interface and Array list is an implementation of that interface. You can safely store the reference of any sub class to
its super reference variable.

This gives freedom to change the behaviour at run time. Also as a general concept we always code for interface not implementation.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is a contract. All classes that implement an interface, promise that they have implementations for all the methods that are declared in the interface.

The interface List has a whole list of method declarations, as you can see in the API documentation.

Class ArrayList is one of the classes that implements interface List, so that means that class ArrayList has implementations for all methods declared in interface List. In other words, an ArrayList is a List - it has everything that a List has.

Therefore, it's type-safe to assign an ArrayList to a variable of type List - the ArrayList can do everything that you can call on such a variable.

In fact, it's good practice to declare the types of variables as the interface types, instead of concrete classes. So, it's better to write: List<String> list = new ArrayList<>(); instead of ArrayList<String> list = new ArrayList<>();.

That is because the rest of the program only needs to know that list is a List; it doesn't need to know what particular List implementation is being used.

By declaring the variable to be of the interface type, it's very easy to change it to a different implementation of List, if that's necessary. You could for example just change that one line to: List<String> list = new LinkedList<>(); and everything will still work - for the rest of the program, list is still a List.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for explaining better than me.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning Tushar,

Thanks Tushar for your reply. I vaguely remember (I might be wrong) something like: an instance of a sub-class can have a name that has the type of its superclass.

However, back to your reply:

"implementation of that interface" = class.

For example,

public class A implements MouseListener { ... }

class A is an implementation of the interface MouseListener.

So my original issue still remains: a class and interface are two distinct types, so how is it possible to assign an object of the ArrayList class to a reference 'list' which is of the type interface?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see Jesper response, he explained everything in very good way. It should give an answer for your question.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A thousand thanks, Jesper.

I see now.

None of this is visited in tutorials.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can take a look at this tutorial.

By the way, if you were not allowed to assign a concrete implementation to an interface variable, you would you be able to use interfaces at all?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:
So my original issue still remains: a class and interface are two distinct types, so how is it possible to assign an object of the ArrayList class to a reference 'list' which is of the type interface?

You may be mixing up your definition of "types".

That class and interface are two distinct types => yes, just as they are distinct from the primitive types int, long, double, boolean, etc.

List and ArrayList are also both types in the sense that they tell Java what kind of object is being referenced. Since ArrayList implements List, this means that to Java a object that is an ArrayList can also be consider a List because the ArrayList guarantees that it provides the capabilities that a List has promised to provide.

It's kind of how Student is a type and you are a Student. At the same time, if you go to the movies, you are also a MovieGoer. So an interface is really a general description of behavior/capability/role, whereas you, being the implementation, can have that behavior/capability/role and still be your own unique self.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavel, Junilu and Jesper,

Many thanks. The issue is I think is resolved. I am greatly excited.

A some months ago, I did this simple experiment, now I see additional reasons why it worked.

* ObjectOne is a frame with a button in it.
* ObjectTwo is nothing more than the all the methods of the MouseListener defined and is used to detect clicks of the button in ObjectOne.

So, ObjectTwo is actually a MouseListener interface despite being declared as a class.




 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're getting hung up on the words interface and class, and confusing those concepts with types.

Type is a concept that the Java language uses to decide whether it is legal to assign a particular object (or value for primitives) to a particular reference. An object can be compatible with many different types at the same time, and can be assigned to any reference type that it is compatible with.

Both interfaces and classes define types for Java to work with, but the concept of interface and class is not part of that type. The object you talk about is not a MouseListener interface, it is a MouseListener.

It is also an ObjectTwo (but not an ObjectTwo class) at the same time.

Does this make sense?
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, thank you Mike.

It does make sense when read slowly!

That's what I find exciting about Java.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!