Solved

Interfaces and objects

Posted on 2013-01-09
5
182 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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 334 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 334 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now