views:

243

answers:

7

Which class implements all the Connection Interfaces which are in javax.microedition.io package and how?

And in the same way which class implements the some of Collection interfaces like Iterator interface. I saw a code: -

Iterator it;
ArrayList list = new ArrayList();

it = list.iterator();

The iterator() return type is "Iterator" which is an interface.

Please tell me what this code is doing is it returning an object of type Iterator? but as far as I know, interface can't be initialized.

A: 

You should use javax.microedition.io.Connector as a factory for Connections. The implementations it provides are not your concern.

Bozho
A: 

It depends on the library you are using, for example if you are using JDBC. You will find JdbcOdbcConnection which implements JdbcOdbcConnectionInterface which extends Connection

You get the connection object through the DriverManager.getConnection which takes a Driver library.

medopal
A: 

Assuming you are talking about java.sql.Connection. JDBC is a specification which defines a Java Interface (several actually). Connection is one of them.

The implementations of Connection are to be found in the various JDBC drivers, provided by he DBMS vendors and independent driver makers.

Diego Dias
+2  A: 

I assume you mean java.sql.Connection? If so ...

The Connection interface is implemented by the JDBC driver provider (Oracle etc.) and the implementation of that interface is instantiated and returned via the java.sql.DriverManager The implementation of this interface would be found in the jar file you are including in your class path for your project.

Malcolm Featonby
the question is about j2me, so java.sql.Connection isn't the first guess.
Bozho
it is now ... when it was first posted it was a completely different question ;)
Malcolm Featonby
+1  A: 

I think you're questioned about connection interfaces that were defined in javax.microedition.io package. (Ignore if this is not the case)

MIDP provides few connection interfaces as like CommConnection, HttpConnection, SocketConnection and etc.

You can get it's instance reference by using Connector. For example,

HttpConnection c = (HttpConnection)Connector.open("http://www.google.com");

We called it GCF(Generic Connection Framework). Please find more information about GCF from http://developers.sun.com/mobility/midp/articles/genericframework/

Wonil Kim
Hey Thanks Wonil KimI'm talking about javax.microedition.io package.I see the following code somewhere but I can't find the implementation of "getLength()" method which is defined in ContentConnection interface..the code is following hc = (HttpConnection)Connector.open(url); int length = (int)hc.getLength();My program is running nicely and giving me the byte length of the image file which I'm using with "url" parameter. But I'm unable to understand from where the length is coming.The getLength() method is not defined in any class.
Bhupi
Hello Bhupi, you don't need to worry about internal implementation. You just can use interface functionality without knowledge of internal implementation.But, if you still want to see what's going on, I recommend to see phoneME open source for JavaME implementation.From it, you can find real implementation of these interfaces. For example, please see Protocol.java file under midp\src\protocol\http\reference\classes\com\sun\midp\io\j2me\http.This is reference implementation of HttpConnection interface.
Wonil Kim
Thanks a lot :)
Bhupi
+2  A: 

Hi Bhupi,

it = list.iterator();

here the iterator() method is returning an object which implements the iterator interface. This is built in feature provided by java.

In J2ME the Connection interfaces, like Bozho said, javax.microedition.io.Connector is a factory class for all kind of connections. And the implementations of those interfaces are again built in feature.

Regards,

wow2010
A: 

Hi Bhupi,

the java sources can be obtained, so you can have a look yourself at what iterator() is doing.

To help you on your way, here's a link to the AbstractList source as implemented by the GNU Classpath team. Classpath is an alternative (open-source) implementation of parts of Java. It may not be exactly how Sun (now Oracle) does it, but it will give you some insight.

I chose AbstractList because that's where the ArrayList.iterator() is implemented.

As you can see, it returns an anonymous (inner) class implementing Iterator.

extraneon