in this, we know that Connection and Statements are Interface's , so how can the objects be created and referred by 'conn' and 'st' objects in the above example even though they are interfaces.. We know that for an interface only subclass objects can be referenced so what about the above case?...
the st.executeQuery() is just signature in the interface so,where is that methods functionality implemented...
Can some one help me in clarifing the doubt .. i Will be very thankful to u if u help me about it...
Basically the reference is of an interface type, which is legal. The actual instance of the object is what the "factory" methods are creating. you don't need to know the specific type of instance, you can use an interface to reference any instance that implements that interface.
That way you code to use any type of Connection class will always be the same and never need to change based on the actual Connection object instance type.
This is called coding to an interface, which is a very powerful too in OO and makes many great Design Patterns possible.
You'll see that the concrete class of, say, conn isn't Connection, but rather something like SqlServerODBCConnection. That class "implements" the Connection interface. In that sense, conn "is a" Connection and can be used anywhere a Connection is called for.
Mark wrote...
That way you code to use any type of Connection class will always be the same and never need to change based on the actual Connection object instance type.
In a word, Polymorphism. That's one of the cornerstones of OO.