Solved

Interfaces and objects

Posted on 2013-01-09
5
187 Views
Last Modified: 2013-03-26
Hello all,

A i understand it, interfaces cannot be instantiated because they have no implemented methods..Therefore, how can we have the following code:

Connection con = null;
con = DriverManager.getConnection(connectionURL );
Statement stmt = con.createStatement();

Open in new window


i.e DriverManager.getConnection(connectionURL ) returns a connection "object"..but objects of Connection cant exist??

Furthermore, the Connection method createStatement() is called, but i thought there could be no implementations for interface methods??

Can someone please clarify please?

Thanks
0
Comment
Question by:oggiemc
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38760286
It's actually quite simple: object O that implements interface I can be (and is in the cases you mention) assigned to type I
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 334 total points
ID: 38761055
An interface defines a set of requirements which you will apply to some class(es). Any class which implements an interface can be assigned to a variable that is of the interface type. You can do this because the compiler knows that the class fulfills the requirements defined by the interface--the class implements the interface.

Think of it like this:  You might say to one of your friends, "I'm going to the store." But what kind of store are you going to? The term "store" is an abstraction of the thing you will be going to. An interface is an abstraction as well. So what might the "store" interface look like?

public interface IStore {
    String getType();  // returns the type of the store
}

Open in new window


So the IStore interface requires that any class which implements it define a method named getType. What might the GroceryStore class look like then?

public class GroceryStore implements IStore {
    public String getType() {
        return "Grocery";
    }

    public String getDeliManagerName() {
        return "John Q. Public";
    }
}

Open in new window


How about the ConvenienceStore class?

public class ConvenienceStore implements IStore {
    public String getType() {
        return "Convenience";
    }

    public int getGasPumpCount() {
        return 6;
    }
}

Open in new window


Both of the above classes implement the IStore interface. You know this because both say "implements IStore", and you can further see that both have a getType method (as required by the IStore interface). We know that both classes above have the the getType method, and we know that the IStore interface requires the getType method. The magic that the compiler permits is that we can create a variable which looks like its an interface type, but in reality points to the concrete instance of one of the classes which implement the interface:

IStore gs = new GroceryStore();
IStore cs = new ConvenienceStore();

Open in new window


Yes, the thing on the left of the equals is an IStore, but this is really more of a pointer to a concrete instance (the things on the right of the equals sign). This pointer, however, only exposes the members its type defines. This means that while either "gs" or "cs" above will allow you to call the getType method, you cannot do either of the following:

String deliMgr = gs.getDeliManagerName();
int pumpCount = cs.getGasPumpCount();

Open in new window


The interface does not know anything about getDeliManagerName or getGasPumpCount, even though the concrete types both variables above point to do know. In order to get that information, a cast would be required:

String deliMgr = ((GroceryStore)gs).getDeliManagerName();
int pumpCount = ((ConvenienceStore)cs).getGasPumpCount();

Open in new window

0
 

Author Comment

by:oggiemc
ID: 38762163
Hi Kaufmed,

Ok that is fair enough, thanks..And i did see these kind of examples before..But,

1) In the my example, there is no concrete instance created i.e there is a method called statically : DriverManager.getConnection(connectionURL );
So how is this the same as if there was an object created i.e InterfaceName inter = new ImplementingClass()??

2) I see the getConnection method in the DriverManager API..However why doesnt it say at the top of this page that "DriverManager implements Connection"??
http://docs.oracle.com/javase/7/docs/api/

3) Statement stmt = con.createStatement();
What class has implemented createStatement(), such that con is allowed to call it here?

Thanks
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
ID: 38762576
1) In the my example, there is no concrete instance created i.e there is a method called statically :
But there IS a concrete instance created, as i explained in my comment:
object O that implements interface I can be (and is in the cases you mention) assigned to type I
However why doesnt it say at the top of this page that "DriverManager implements Connection"??
Because it doesn't implement Connection. You are able, with that class, to get a class that does implement Connection
What class has implemented createStatement(), such that con is allowed to call it here?
Well you don't really need to know that (that's one of the main points about interfaces) but if you're curious, you can find out with
System.out.println(con.getClass());

Open in new window

The same can be done with the Statement implementation
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 334 total points
ID: 38762698
In the my example, there is no concrete instance created i.e there is a method called statically : DriverManager.getConnection(connectionURL );
Why would you think that static methods cannot create instances? Static methods cannot access instance data, but they can certainly create (and return) instances--even instances of the class which they belong to.

The DriverManager class is an example of a factory class.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 52
iterator/ListIterator approach 17 51
Html Table Looping (part 2) 5 34
Tomcat 9 + java 8 error while trying to deploy a war file 2 687
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question