Re: DriverManager in JDBC



asit wrote:
In java [sic], we can't instantiate DriverManager class becoz [sic] the
constructor is private.

Why this is so ??? [sic]

Why the constructor is provided ??? [sic]

You only need one question mark at the end of a sentence to indicate
an interrogative in English.

See Joshua Bloch's excellent book /Effective Java/, Item 1, "Consider
static factory methods instead of constructors" for a detailed
explanation of this idiom.

The short version is that 'java.sql.DriverManager' is a class that you
should never instantiate. It is a utility class that has no state of
its own and all its methods are static (class level). It makes no
sense ever to have an instance of the class, so the API designer
ensured that no client can instantiate the class.

It also prevents specious inheritance of the class. ("Design and
document for inheritance or else prohibit it", /ibid./) It makes as
little sense to inherit from a class of nothing but static methods as
it does to instantiate it.

Some of the methods in 'java.sql.DriverManager' are factory methods
that return implementation instances by their interface type. By
keeping the actual concrete type hidden, the factory has the
flexibility to provide any implementing type, even a different one
from time to time, without damage to client code. This is part of
what is sometimes called, "programming to the interface" or
"programming to the type", a best practice of defining behaviors at
the interface level and generally hiding the concrete classes
involved.

--
Lew
.



Relevant Pages

  • Re: Types of inheritance
    ... Whenever an instance of a derived class is created, the constructor ... Though the terms "interface inheritance" is correct ... the word "inheritance" typically ... If no explicit call is present to a superclass constructor in Java, ...
    (comp.lang.java.help)
  • Re: basic serialization question
    ... > can't instantiate an interface, so what good is a constructor? ... The compiler already throws an exception when you call a constructor on a ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Getting a list of Classes in a Package
    ... instantiate all classes which implement a certain interface and have a certain ... constructor, though. ... If you read both my explanations, you would see that this is what I'm ...
    (comp.lang.java.programmer)
  • Re: Declaring a Constructor in an Interface?
    ... >I am accessing my database through an interface, ... The usual solution to this is to define some kind of factory interface, ... a particular class to provide a particular constructor. ... constructors on the derived class are those you write explicitly. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What about CAO?
    ... So if I have the remoting service set as SingleCall - it's ... it's not possible to instantiate a new ... Now implement this interface on the ... > only on the client). ...
    (microsoft.public.dotnet.framework.remoting)