I have 2 classes. Let them be called class A and class B. Class A contains a method that performs an action. Class B overrides this method with its own version, but makes a super call on this method in class A to complete the action. Now it works fine. However, in class A there are some actions that should be performed only if the object is only an instance of class A. In another way, some actions in the method of class A should not be performed if the object is an instance of a child of class A.
I am currently using instanceof to check each child, but I need to specify each child class, so if a new child is added later, this method needs to be updated. I would like a dynamic way to determine if an object is a child.
Are there any good alternatives or is there instanceof way?
public class A{ public void someMethod(){
As an explanation of the design, I use it to create XML. In the program, I have a List<A> for storing data. When it's time to output the XML, I go through the list and call generateXML ( someMethod takes its place in my example).
When an object of class A is created, it must have its data in the <A></A> tags. When an object of class B is created, it must have its data in the <B></B> tags. But all properties of A must also be inside <B></B> tags, so right now it calls the same generateXML method that is used when an object has only class A
But, as some others have pointed out, calling the same method is not the way to go. Class B must invoke a protected method in class A that generates only the necessary information.
source share