aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two ways of implementing multiple interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Two ways of implementing multiple interfaces" Watch "Two ways of implementing multiple interfaces" New topic
Author

Two ways of implementing multiple interfaces

Murgan Sub
Greenhorn

Joined: Feb 06, 2002
Posts: 22
Have a look at this code below
Apparently there are two ways of implementing
multiple interfaces.
One with a single class other with inner class.
interface M {}
interface N {}
class A implements M, N {}
class B implements M {
N makeN() {
// Anonymous inner class:
return new N() {};
}
}
public class MultiInter {
static void takesM(M m) {}
static void takesN(N n) {}
public static void main(String[] args) {
A x = new A();
B y = new B();
takesM(x);
takesM(y);
takesN(x);
takesN(y.makeN());
}
}
What are the advantages of using an inner class
over a single class ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Murgan Sub:
What are the advantages of using an inner class
over a single class ?

I'm not sure I've ever seen a real life example that works quite like you've shown here, but I'll take a shot at one that's fairly close.
Think about the interface Iterator. Let's say you're creating a data container and you'd like to be able to provide the functionality of an iterator. You could just have the data container itself be an iterator:

There's a problem with this, though. What if two separate threads call the iterator method on your container and start looking through the container? Since you really just passing around a single instance of a class, there is only one "iterator pointer" that states where the iterator is "looking into the data." When one thread calls next(), all other threads are suddenly looking at the next element, even though they never called next().
You can, however, use an inner class to solve the problem.

Now, any thread that calls the iterator method gets its own instance of an Iterator object, which has its own counter. Of course, because it is an inner class, it has full access to the data contained by the enclosing class, which allows it to return data to any requestor.
This is just one way you can use an inner class. As I said, this doesn't exactly match what you originally asked because the example you gave showed a class "implementing" an interface by returning an inner class that really implemented that interface. In this case, our data container doesn't claim to implement that interface. I can't think of a case where that would really be useful (or not confusing, for that matter).
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
hi Murgan for the code you posted I can not see any difference.
But imagine M and N are not interfazes but classes. The approach of nested classes is now needed. This is a kind of multiple inheritance.


SCJP2. Please Indent your code using UBB Code
Murgan Sub
Greenhorn

Joined: Feb 06, 2002
Posts: 22
M and N are interfaces.
Look at this code below.
This is a way of implementing an interface
using an Inner Class
interface Inter {
int value();
}

public class Example {
public Inter cont() {
return new Inter() {
private int i=12;
public int value() { return i; }
};
}
public static void main(String[] args) {
Example p = new Example();
Inter c = p.cont();
}
}
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There is a subtle difference though.
Suppose there are two inner classes implementing the same interface. They could do it in different ways. For doing that when a class implements an interface, the class should implement several interfaces. In the second example the implementing class really "IS" both interfaces. In the first one only can be casted to one. You are adding behaviour without modifying the type.
[ February 12, 2002: Message edited by: Jose Botella ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two ways of implementing multiple interfaces