Disclaimer….I’m new to Java and OOP.
I have a superclass with four subclasses. For each subclass, I need to parse an XML file. About 40%-50% of the elements in each XML are identical to one another, with the other half different for each subclass.
My initial approach was to include the SAX parser handler code in the superclass and simply list all possible XML elements into it. But then I faced the issue of communicating the other 50%-60% of elements specific to the subclasses back down to the subclasses. Since the superclass does not know about each subclass (I think), I think I would violate OOP principles if I somehow force that data down to the subclasses.
So now I’m thinking I might need to set up four different SAX parsers, one in each subclass. The super class would still be used for common instance variables and other methods. But I would also have quadruplication of some SAX parser code.
Any advice on how to proceed and stay true to OOP principles?
For this level of complexity I would recommend an alternate approach to mapping the XML to your object layer. Check out JAXB – it provides a very robust framework for these kind of mappings and handles object inheritance automatically.