• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface as an Object?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm having trouble understanding the concept of the interface Connection, and PreparedStatement.

1) The simplest way to put it is how is it possible that this code is creating Connection and PreparedStatement objects? I was always under the impression that interfaces cannot be instantiated, but rather implemented. For example I don't see "public class Prepared implements Connection", or "public class Prepared implements PreparedStatement", But I see "Connection con = null;" and "PreparedStatement pst = null;". So it seems as if the interfaces are being used to create objects called con and pst.

2) If in fact these interfaces are being implemented, where are the method blocks in this code that should have been added in order to fulfill the contract?


Code i'm referring to is below. I appreciate the help.

- Ian

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Ian Lafuente wrote:But I see "Connection con = null;" and "PreparedStatement pst = null;". So it seems as if the interfaces are being used to create objects


Those statements do not create any objects. They create references named con and pst (I hate freeze-dried names, suggest using better names) that are references to interfaces. The objects aren't created until later.

And when the objects are created, they are created as implementations of the interfaces. You just can;t see it cause it's done in code you call rather than have written.
 
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest way to put it is how is it possible that this code is creating Connection and PreparedStatement objects? I was always under the impression that interfaces cannot be instantiated, but rather implemented

The code isn't creating an instance of the Connection interface it's creating an instance of a class that implements Connection. You don't need to know what the class name is you just need to know you are being given a reference to an object that has the API defined in the Connection interface. The same applies to PreparedStatement.

Edit: Too slow.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, in this statement:the getConnection() method is creating a object and returning it for assignment to con. For this to work, the object must be an implementation of Connection.

As Tony pointed out, what class it actually is is irrelevant. That's the beauty of interfaces. The database driver can create any class as long as it implements Connection, and we don't have to care.

I ❤ me some interfaces!
 
Ian Lafuente
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked up the DriverManager class (java.sql.DriverManager), but i'm still confused. I don't see where the object of class DriverManager c implements the Connection interface. It doesn't include all the methods from the Connection interface in this class.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't see it. It's likely buried deep in the heart of the specific database driver for whatever database you are using -- which you most likely do not have the source code for.

But that's not necessary to understand the concept. The simple concept is that code that you call is creating a class that implements the interface. All you care about is that the returned class implements the interface. How the class was created, and what specific class it is, is irrelevant.

Simple example. Let's say that class Pomeranian implements interface Barker. A method in a class named DogFactory to create an instance of Barker could be:
In calling code, you might see:
This code has no idea -- and doesn't need to know -- that the barker is a Pomeranian. All it it knows, and cares about, is that it is a Barker.

A different version of DogFactory might create an instance of GermanShepherd and return that. As long as GermanShepherd is also a Barker, the calling code could care less.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. My Pomeranians are currently barking at the UPS man, hence the specific example.
 
Ian Lafuente
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean to write dogFactory.makeBarker() instead of barkerFactory.makeBarker()?
So in your example, the Pomeranian class, which implemented the interface Barker, contains all of Barkers methods? barker is of the Barker type, and makeBarker() creates a new Pomeranian object which is then assigned to barker?

Going back to the code, "Connection con = null" and "con = DriverManager.getConnection(url,user,password);"
con is of type Connection and the getConnection() method in the DriverManager class will create a new object (of a class that implements Connection) that will be assigned to con?

I think i'm starting to understand it. What I don't understand is that looking at the Java API where DriverManager is defined (http://docs.oracle.com/javase/6/docs/api/) I don't see anywhere that indicates that the method getConnection() creates a new object (which would be of a class that implements Connection).


By the way, thanks for answering all of my questions quickly and easily.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Lafuente wrote:Did you mean to write dogFactory.makeBarker() instead of barkerFactory.makeBarker()?

Yes. Sorry, fixed. No compiler ;)

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Lafuente wrote:So in your example, the Pomeranian class, which implemented the interface Barker, contains all of Barkers methods?

Yes. It has to if it declares that it implements Barker.

barker is of the Barker type, and makeBarker() creates a new Pomeranian object which is then assigned to barker?

Yes.

Going back to the code, "Connection con = null" and "con = DriverManager.getConnection(url,user,password);"
con is of type Connection and the getConnection() method in the DriverManager class will create a new object (of a class that implements Connection) that will be assigned to con?

Yes. (Except it doesn't have tho be a new object -- see below.)

What I don't understand is that looking at the Java API where DriverManager is defined (http://docs.oracle.com/javase/6/docs/api/) I don't see anywhere that indicates that the method getConnection() creates a new object (which would be of a class that implements Connection).

We don't know if it creates a new object or not. All we know is that it returns an object that implements Connection. Whether it's new or not is irrelevant. It may return a Connection from a pool.

The interface "contract" just mandates that the method return a Connection. (or null)
 
Ian Lafuente
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bravo Bear! Thank you very very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are most welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!