I have 5 different tables in a database. I have written an abstract "Converter.java" class that take out data from the database and convert it into a "tree.xml" file.
Tree.xml
<?xml version="1.0" standalone="no"?>
<tree>
<declarations>
<attributeDec1 name="name" type="String"/>
</declarations>
<branch>
<attribute name="name" value="process 1"/>
<leaf>
<attribute name="name" value="process 2"/>
</leaf>
<leaf>
<attribute name="name" value="process 3"/>
</leaf>
</branch>
</tree>
So as you can guess, the structure of the above tree.xml will remain same for all the 5 tables. The only difference is in the value of attribute "value".
To get the value of that "value" attribute, the converter first have to query the database for those values.
So, there should be 5 different queries for 5 different tables. So instead of coding 5 different converters, I have made a singele "Converter.java" that has functions like
- openTree()
- closeTree()
- openBranch()
- closeBranc()
- openLeaf()
- closeLeaf()
- addAttribute()
Out of the above methods, I have implemented all the methods except addAttribute() method as it will have implementation depending on the table.
After that, I have coded 5 different converters, each for one table and all of them extends "Converter.java"
I have made a field named as "query" in the Converter.java class. As this field is inherited by all the 5 Converters, I have initialized this field in the constructors of those 5 converters.
Now my question is as follows:
Q. I am not confident that whether I have used inheritance correctly or there should be some changes in the above approach?
Edit:
Q2. I have one more method in Converter.java class that uses "query" field. As the field is not initialized in the Converter.java class, so whether I copy that method in all the 5 child converters or it is okay to put that method in the parent class (i.e. Converter.java)