This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Interface? Pls. help!

 
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
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 ]
 
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
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!