aspose file tools*
The moose likes Beginning Java and the fly likes Interfaces v/s Imports. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces v/s Imports." Watch "Interfaces v/s Imports." New topic
Author

Interfaces v/s Imports.

Ramnath Krishnan
Ranch Hand

Joined: Feb 10, 1999
Posts: 37
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

Joined: Oct 17, 1999
Posts: 117
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)


Saravanan
Ramnath Krishnan
Ranch Hand

Joined: Feb 10, 1999
Posts: 37
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

Joined: Nov 28, 2005
Posts: 114
hi all,
please extend this thread


Thanks,
www.Admakr.in
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Babu, you have just resurrected a thread that is almost six years old!

Can you please explain what exactly you would like to know?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
Even I am interested in knowing where is the implementation of all those interfaces written.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108

"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_


Mike Noel
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces v/s Imports.