• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appropriate use of inheritance  RSS feed

 
Yuma Shankar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have many Database Access Classes (D1, D2, D3...) for each table in our application. All of these classes need to access some common functions such as getConnection and getResultSet.

I suggested keeping these functions in a separate Utility Class. The concern in the team is if we do so we will instantiate several objects of the Utility class as all D1, D2, D3 etc. will access these.

Another team member suggested that we should have a baseclass DBase for all the Database Access classes i.e. have D1, D2,D3 extend DBase and that will allow us to access the common functions and we will avoid creating several instances as in the case of the Utility class.

I find it odd that we should use inheritance in this manner where there is really no "is-a" relationship whatsoever betwen the base class and the derived classes!

Is my concern valid and Can someone come up with any other alternatives?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not do both?

The is-a relationship is a matter of perspective. If all of your objects require this method, its easy to make an argument that it is part of their being.

So if each object should be able to get the connection is a design decision. If objects should be given the connection when created is a design decision.

A utility class will result in the same design. Instead of passing the dao classes the info necessary to open a connection, you will be passing them a utility class which can get the connection for them. Thats probably a better idea as getting data and opening connections are kind of different things.

I'd say do both.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never uses concrete inheritance* (declaring a class non-final) - always use interface inheritance.
http://www.jtiger.org/articles/why-extends-is-not-evil.html

*with the exception of exceptions, which unfortunately are classes, but should be metadata.
 
Yuma Shankar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am actually considering just making the utility class a class with static functions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!