Reputation: 47
Is it possible to extend a class with a nonvisible constructor without having to know what this constructor do and implement it again?
import java.sql.DriverManager;
public class myDriverManager extends DriverManager {
public myDriverManager() {
super(); //Fehler: the constructor DriverManager() ist not visible
}
}
Upvotes: 2
Views: 181
Reputation: 2405
Every constructor's first line is either a call to its super class constructor or to any current class constructor, so even if you don't keep it compiler will keep a super();
as first line.
Now coming to DriverManager
its only constructor is private
and when you extend it to your class, its constructor will try to call the DriverManager
's constructor as per above logic and gives Compile time error since its private and becomes not visible.
The case will be same even if you don't declare the class without constructor
public class myDriverManager extends DriverManager {
}
This will also give the same error, because the compiler will create a default constructor and its first line will be super();
by default, again by the above logic, constructor is again invisible.
So basically what happens is when you extend DriverManager
your class has to call super();
at some part of its code, and gives compile time error and hence DriverManager
cannot be extended to any class.
Upvotes: 2
Reputation: 58892
You can't extend DriverManager, but you can implement DataSource
instead
NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to connect to a data source. The use of a DataSource object is the preferred means of connecting to a data source.
Upvotes: 4