Solved

Interfaces and objects

Posted on 2013-01-09
5
184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
Need to track down Infection in a Server 2008 domain user profile 7 48
eclipse argument 14 53
Java Loop 6 49
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now