Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Is this a correct implementation of an abstract class and implementation class Date: Sun, 13 Aug 2017 07:14:45 +0200 Organization: Decebal Computing Lines: 64 Message-ID: <87a834xd4q.fsf@munus.decebal.nl> References: <87r2wgxz2p.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="1615173a1409002a85fbdd25ac4b9ead"; logging-data="17580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181RmdZTQHe6uYxY2KDKjhQNZzSMkf2xIM=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:NTjI5GWTGMDuhSDVIT7Gmk3mxKQ= sha1:K55GDaNmou2+BcAvzcvN98/eie0= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:37160 Arne Vajhøj writes: > On 8/12/2017 5:20 PM, Cecil Westerhof wrote: >> I want to write a layer around JDBC. For this I have an abstract class >> SQL in which I have the following constructor and abstract methods: >> // ################ constructors >> public SQL (String database) throws SQLException { >> String forName = getForName(); >> String sqlDriver = getSQLDriver(); >> >> try { >> Class.forName(forName); >> } catch (ClassNotFoundException e) { >> throw new SQLException("Could not find the class " + forName + "."); >> } >> conn = DriverManager.getConnection(sqlDriver + database); >> this.database = database; >> } >> >> // ################ abstract >> abstract String getForName(); >> abstract String getSQLDriver(); >> >> >> I have also an implementation class SQLite3 in which I have: >> // ################ constructors >> public SQLite3 (String database) throws SQLException { >> super(database); >> } >> >> // ################ default >> @Override >> String getForName() { >> return "org.sqlite.JDBC"; >> } >> >> @Override >> String getSQLDriver() { >> return "jdbc:sqlite:"; >> } >> >> Is this the correct way to do this, or is there a better way? > > I have done something similar many times. > > I like the approach. Thank you. > I assume you are aware that with newer Java versions then > Class.forName is not required. Not, I was not. I removed it. > And I would rename getSQLDriver to getURLPrefix. Good tip: done. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof