Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface? Pls. help!

 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:

=========================================================

package monitor;

import java.util.Collection;

public interface AbstractMonitorProcessor {
public Collection getAll();
}

========================================================

package monitor;

import java.util.ArrayList;
import java.util.Collection;

public class DestinationProcessor implements AbstractMonitorProcessor {
ArrayList _list = new ArrayList();
public void addElement(Destination destination) {
_list.add(destination);
}

public Collection getAll() {
return _list;
}
}

=======================================================
package monitor;

import java.util.ArrayList;
import java.util.Collection;

public class ClientProcessor implements AbstractMonitorProcessor {
Vector _vector = new Vector();
public void addElement(ClientData clients) {
_vector.add(clients);
}

public Collection getAll() {
return _vector;
}
}

I have three questions:
1. Why the method getAll() in the interface class returns Collection?
2. What is the advantage of that?
3. If I call the getAll() method in DestinationProcessor class, how do
I know it actually returns an ArrayList vs. Vector as in the case
of ClientProcessor class?

Thanks in advance!
BL
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64836
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why the method getAll() in the interface class returns Collection?

Collection is an interface implemented by many elements in the Java collections classes.

2. What is the advantage of that?

By having the interface return a Collection, the implmenting class is free to use whatever collection type it deems most appropriate as long as that collection type implements Collection.

3. If I call the getAll() method in DestinationProcessor class, how do I know it actually returns an ArrayList vs. Vector as in the case of ClientProcessor class?

You don't. And you shouldn't care. The fact that an ArrayList was used is moot. You treat whatever is returned as a Collection and are freed from having to know the implementation details.

Isn't that simply a wonderful thing? Man, I love interfaces!
[ August 25, 2004: Message edited by: Bear Bibeault ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, Bear, why did you answer that so completely? My super-human senses seem to indicate a copy-paste from a homework.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64836
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, that one didn't set off my homework radar. Probably because the questions aren't very well worded and questions 1 and 2 are really one question. But I could be wrong.
 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear for the answers.
BTW, this is not a copy-paste from a homework!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic