• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces v/s Imports.

 
Ramnath Krishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A Class implementing an Interface must always implement all the method in the interface.
Eg.. an interface called Sortable has a sort method and a class called myClass implements Sortable, it must implement the sort method.
Code Sample
<HTML>
public interface Sortable

{

public void sort(anObject a);

}

public class myClass

{

public void sort(anObject b) { // code here }
public static void main(String[] args) {

anotherObject a = new anotherObject();

anObject b = new anObject b;

a.sort(b);
}

}

</HTML>
However, when the interfaces are present inside a package, eg.
ResultSet interface is present under java.sql.*
so
when you write a code that implements the ResultSet interface you never implement method such as next(),close() in your code.

Code Sample.
<HTML>
import java.sql.*;

public class myClass

{

ResultSet rs = null;

// connection etc..

while(rs.next())

{

//some code here

}

}
</HTML>
So, does anyone know how this is possible and how the next(),close() works as the interface cannot write the implementation.
So which class has the implementation to close(),next() etc.
This question is applicable to all the interfaces.
Thanks.
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramnath Krishnan:
Hi,
A Class implementing an Interface must always implement all the method in the interface.
...
...
...
code that implements the ResultSet interface you never implement method such as next(),close() in your code.


Your code (class) doesn't seem to implement any interface?
Note that interfaces canbe used inside a class without implementing. And if you implement a interface, you have to provide the implementation. There are some tag-interfaces (like Serializable, Remote) which require no implemetation. (they have no body, it is only for the JVM)
 
Ramnath Krishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct my class does not implement any interface, but I use it by importing it.
But when you declare an interface, you cannot write an implementation, so where is the implementation for next(),close() ....etc written.
How do I get an instance of ResultSet object or someother interface such as Statement's executeQuery method returns an ResultSet object?
In your example, Interfaces such as Serializable etc.. do not have any methods to implement but in the code you write
class a implements Serializable,Cloneable....
but in my example I do not write implements but i do an import.
Sorry about this, I am just a bit confused here.
Also could you please explain the difference when you said that I can use an interface without implementing it.
(So if I were to create my own package of interfaces and then
imported it in my class, i can use it without implementing it...
but still I cannot write any method implementation within the interface..?
Thanks.

 
A Babu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
please extend this thread
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Babu, you have just resurrected a thread that is almost six years old!

Can you please explain what exactly you would like to know?
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I am interested in knowing where is the implementation of all those interfaces written.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rama murthy:
Even I am interested in knowing where is the implementation of all those interfaces written.

If you want to find out the name of the class implementing an interface:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No one answered the question six years ago, and Jeff has only given a hint, so...

The java.sql package (for example -- a good example!) consists almost entirely of interfaces. ResultSet, Statement, Connection, etc. When you work with JDBC, you use these interfaces constantly, without ever seeing an actual class that implements them. The question is, how does this work?

If you use JDBC, surely you are aware of JDBC drivers. You may not know, however, exactly what a JDBC driver really is: it's a collection of non-public classes that implement the java.sql interfaces! These implementations do whatever is needed to talk to a particular vendor's database.

If you look carefully at JDBC, you'll see that one of the few real classes is DriverManager. To connect to a database, you call

Connection c = DriverManager.getConnection ("jdbc:mysql:///DB");

The DriverManager looks in a Collection of Driver objects, which are registered when you do the traditional Class.forName("com.package.DriverClass") thing. These classes implement the java.sql.Driver interface. The DriverManager asks each one if it accepts the URL by calling acceptsURL(), then if the driver returns true, calls connect(). The connect() method returns an instance of the class in the driver that implements Connection, and from there, the driver takes over.

Now any time you call a method on that Connection that returns a JDBC object, the Connection (which is an instance of some class provided by the driver with a name like MySQLConnection, for example) returns an instance of the driver's implementation of that interface.

Make sense? The reason why this is a good thing is that every driver can be implemented its own way, and you can work with all of them because you only use interfaces.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool explanation, Ernest. To focus on one line:

Connection c = DriverManager.getConnection (connectionString);

Connection is an interface. All you know about the object returned by getConnection is that it implements the interface so you can call any method on "c" that is defined by the interface. The actual implementation class might be one for mysql, db2, oracle or any other db vendor.

"Coding to the interface" is a very cool concept that most design gurus encourage you to use liberally in your own code. Note that we could install a new database with a new driver and never touch your code that uses Connection. This "decoupling" helps keep systems flexible and easy to change over time.
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Coding to the interface" is a very cool concept that most design gurus encourage you to use liberally in your own code. Note that we could


Not to derail this particular topic, but the OpenOffice API uses this concept extensively and at a very fine grained level. If you're interested in the "coding to the interface" idea you might want to check it out.

_M_
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Noel:


Not to derail this particular topic, but the OpenOffice API uses this concept extensively and at a very fine grained level. If you're interested in the "coding to the interface" idea you might want to check it out.

_M_


You don't have to go to a third-party library to find examples of this concept. As illustrated above, the Java API is chock full of examples of coding to the interface. JDBC, Swing, and the Collections Framework all use this idea extensively. The cool thing about this concept is that we typically don't even care where the implementation is or the details of said implementation. As long as it fulfills the contract described in the documentation, the class that implements an interface is typically not important.

Layne
[ January 13, 2006: Message edited by: Layne Lund ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic