Returning an interface?
I was sure you could not return an interface, but only an object, primitive or void, but i noticed on the Java platform site that the List class has a ListIterator method that returns a listIterator.
But how can this be since ListIterator is an Interface?
[269 byte] By [
Jahvaha] at [2007-11-27 10:35:53]

returning an interface is often done. You are returning an abstraction. The returned object can be anything at all that implements that interface.
ah ok thanks. I think what confused me was that under ListIteratror there was no "classes that implement this inteface". So i couldn't think of the class of the object that was getting returned. It must be a secret class..
Message was edited by:
Jahvah
I knew you could make a return statement with a Interface variable, but that variable must still be an instance of a class and on the java site it says the return type is ListIterator which is an interface.
I'm confused.
null
Here is a totally incomprehensable example:
The interface
interface MyI
{
void setInt(int i);
void doSomething();
}
the class that implements the interface
class Foo implements MyI
{
int i;
public void setInt(int i)
{
this.i = i;
}
public void doSomething()
{
System.out.println("doSomething in class foo " + i);
}
}
the class that uses the class that implements the interface
import java.util.Random;
public class Fubar
{
private MyI[] myIs; // array of interface myI
public Fubar()
{
myIs = new Foo[10];
for (int i = 0; i < myIs.length; i++)
{
myIs[i] = new Foo();
myIs[i].setInt(i);
}
}
// method randomly returns one of the 10 myI interfaces in the array
public MyI returnMyI()
{
Random rand = new Random();
return myIs[rand.nextInt(myIs.length)];
}
public static void main(String[] args)
{
Fubar foo = new Fubar();
MyI interfaceVariable = foo.returnMyI();
interfaceVariable.doSomething();
}
}
the house that jack built.....
Message was edited by:
petes1234
> I knew you could make a return statement with a
> Interface variable, but that variable must still be
> an instance of a class and on the java site it says
> the return type is ListIterator which is an
> interface.I'm confused.
You can write a class with a method that returns a List, like this:
public class InterfaceReturner
{
private List data = new ArrayList();
public static void main(String [] args)
{
InterfaceReturner ir = new InterfaceReturner();
for (int i = 0; i < args.length; ++i)
{
ir.addData(args[i]);
}
List test = ir.getData();
System.out.println(ir);
}
public List getData() { return data; }
public void addData(String o) { data.add(o); }
public void removeData(String o) { data.remove(o); }
}
The private data member for this class is of type List. That's all the client needs to know. The implementation underneath happens to be of type ArrayList, but I could have made it any other class that implements the List interface.
All the client cares about is calling the methods available in the List interface.
The class can change the private implementation from ArrayList to LinkedList without affecting the client
%
The key is to think about what you're doing when you declare a variable name. You're setting aside bytes in memory to store the variable's reference AND you're saying it is a particular type. If it's a class or interface type, you're allowed to call the methods that are accessible by you. If you happen to be an interface type, you can only call the interface's methods.
So when someone returns a reference to an interface type, they're saying that you can only call interface methods and they promise to worry about the implementation. How those methods are implemented is immaterial to you.
%
> I knew you could make a return statement with a
> Interface variable, but that variable must still be
> an instance of a class
The variable holds a reference to an instance of a concrete class.
> and on the java site it says
> the return type is ListIterator which is an
> interface.
So it returns a reference to a ListIterator. A concrete class that implements ListIterator IS-A ListIterator, and it returns a reference to and instance of that implementation.
jverda at 2007-7-28 18:37:44 >

> Wellcome back duffymo
Thank you.
%