aspose file tools*
The moose likes Beginning Java and the fly likes Interface? Pls. help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface? Pls. help!" Watch "Interface? Pls. help!" New topic
Author

Interface? Pls. help!

Bob Green
Ranch Hand

Joined: Feb 29, 2004
Posts: 93
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

Joined: Jan 10, 2002
Posts: 61764
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Out of curiosity, Bear, why did you answer that so completely? My super-human senses seem to indicate a copy-paste from a homework.


I've heard it takes forever to grow a woman from the ground
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

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

Joined: Feb 29, 2004
Posts: 93
Thanks Bear for the answers.
BTW, this is not a copy-paste from a homework!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface? Pls. help!