Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Interface as return type to a Method

Hi All,
I am working on a maintanance project. I have to go thru the code and understand it. I came across a code and I am not able to understand it.

I have two Interface A, B. A has many methods. B has one method whose return type is "A". What does this mean and what is the advantage of this.

Please explain this concept.
[345 byte] By [balamemory] at [2007-11-11 6:41:29]
# 1 Re: Interface as return type to a Method
Do u know how EJB's work...thy have 1. remote interface, 2.home interface 3. ejb class..

home interface defines the create method and it's return type is remote interface.. so just go through the ejb' interfaces ,i think u may understand the problem.. :WAVE:
need at 2007-11-11 22:40:29 >
# 2 Re: Interface as return type to a Method
if u understand fully..just send the info . in this forum..
need at 2007-11-11 22:41:34 >
# 3 Re: Interface as return type to a Method
I have two Interface A, B. A has many methods. B has one method whose return type is "A". What does this mean and what is the advantage of this.

I assume you understand the concept of interfaces....

If, say, a class ABClass implements A and B then an instance of that class
may be used like this:

// in ABClass
someOtherClassInstance.useBClass(this);

// in the other class the parameter is for the B interface
public void useBClass(B b) {
A a=b.getA(); // gets the A interface and uses its methods
a.anAMethod();
a.anotherAMethod();
}
sjalle at 2007-11-11 22:42:33 >
# 4 Re: Interface as return type to a Method
Read this. It explains it pretty good.

http://java.sun.com/docs/books/tutorial/java/interpack/interfaceAsType.html

basicly that method in B is just saying that its gona return an object that implements the A interface, and that object will have all the methods of the A interface.
ZeroFear at 2007-11-11 22:43:33 >
# 5 Re: Interface as return type to a Method
That is true. As for the java interface concept (from your tutorial reference),
it was included in the java language to have the C++ friend and multiple
inheritance concepts' benefits, without compromising the OOP standards.
sjalle at 2007-11-11 22:44:37 >