samedi 24 janvier 2015

Overriding superclass attributes/variables in Java, Android

I have two databases which look identical in their layout i.e same column headings, same read/write methods but store data related to different aspects of my application.


I am trying to use one database class as the superclass and have the other database extend this class so that I do not have to rewrite the whole class, and so that changes to the database manager do not have to be made in two different classes.


My problem is that I want both databases to have different names and therefore be stored in different files. When I specify the private static final DATABASE_NAME in the subclass and invoke the superclass constructor (which invokes the SQLiteOpenHelper constructor) the DATABASE_NAME from the superclass is used, storing all data in the same file as the other database.


I want to override this attribute so that I can change the database name in subclasses but retain the methods in the superclass.


I understand that overriding class attributes is not allowed in java and that this should be done through overriding class methods, however due to the static nature of the variable I cannot override a getter method that returns it as this would yield the same result.


Is there any way around this or will I have to recreate the class in the subclass?


Thank you.


Aucun commentaire:

Enregistrer un commentaire