• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces v/s Imports.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
please extend this thread
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even I am interested in knowing where is the implementation of all those interfaces written.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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_
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic