Subclass/interfaces

I must be having a brain fart because this feels like something I’ve easily accomplished before, but right now I’m drawing a blank trying figuring it out.

I want two database subclasses (a subclass of MySQLCommunity server and a subclass of SQLiteDatabase) to have their own methods for connecting, but many shared methods for modifying themselves via SQL. The tricky part is that I want to be able to pass either as the same datatype in a function.

I have a DoConnect method that is different for each DB type, and then many “extra” methods that are exactly the same for all database types. If I try to create a class interface, I would have to implement the “extra” methods identically for each class, which would cause needless repeated code. If I try to subclass Database to add the DoConnect method there, I can’t specify the actual DB type for each DB in its own subclass, as the super would be the same for both.

Extends methods also don’t help much as you can’t use extends ClassInterface to use methods/properties that are common to all databases.

Thanks for the help.

You need three classes. A common super class that contains all the “extra” methods and an empty DoConnect method. Then subclass it for each of the specific database types, where you add a specific DoConnect method that overrides the empty one in the super.

Right that’s what I initially thought, but the super for each subclass can either be the common super class or MySQLCommunityServer, SQLiteDatabase, etc. I can’t make the subclass both a subclass of the common super class AND the specific database class.

You cannot subclass a database in any meaningful way. You have to have a class that contains a property of type MySQLCommunityServer or SQLiteDatabase. (I use type Database and put the property in the common super, then cast it as appropriate when I need to use it in a database-specific manner.)

Now that I think about it, I don’t actually have subclasses, just the main class that has all the normal database api methods, which just pass through to the internal property, as well as my custom stuff. I set a Type property and use that when I have to cast to a specific database type (or you can use ISA, but this was cleaner at some ancient stage of development, so I stuck with it).

Thanks, sounds like a good possible solution. In the meantime I wrangled a way to get what I was originally attempting to do, which may or may not prove useful to someone in the future:

  1. I made a class interface AnyDB that has the DoConnect method
  2. I made subclasses of the database types that implement the AnyDB interface DoConnect method for their specific database type
  3. Added extends methods that extend the AnyDB class interface, and then cast it as a generic database object to do the common methods that require SQLSelect/etc.

Well I’ve not tried it out, but my approach would be to:

Implement the common stuff as extends of the Database class (throwing not implemented exception if necessary depending on the actual class being operate upon) and the DoConnect as an extension of the two database types involved.