Check if Calling Object is Instance of Child Class

compuguru :

I have 2 classes. Let's call them class A and class B. Class A contains a method that executes some action. Class B overrides this method with its own version, but does make a super call to that method in class A to perform an action. Right now, this is working fine. However, there are some actions in class A that should only be executed if the object is only an instance of class A. Put another way, some actions in the Class A method should not happen if the object is an instance of a child of Class A.

Currently, I'm using instanceof to check for each child, but I need to specify each child class, so if a new child is added at a later date, this method needs to be updated. What I would like is a dynamic way of determining if the object is a child class.

Are there any good alternatives, or is instanceof the way to go?

public class A{

    public void someMethod(){

        // Only perform these actions if it is not a child class.  This is what
        // I am looking for a better solution for
        if(!(this instanceof B)){
            // Some action...
        }

        // Actions to execute for every call
    }
}

public class B extends A{

   @Override
   public void someMethod(){

       super.someMethod();

       // More actions
   }
}

Just as an explanation of the design, I am using it to generate XML. In the program, I have a List<A> to store the data. When it is time to output the XML, I loop through the list and call generateXML (the someMethod takes its places in my example).

When an object of class A is created, it needs to have its data within <A></A> tags. When an object of class B is created, it needs to have its data within <B></B> tags. But all the properties of A must also be inside the <B></B> tags, so as of right now it calls the same generateXML method used when an object is only of of Class A

But as some others have pointed out, calling that same method isn't the way to go. Class B should be calling a protected method in class A that only generates the necessary information.

cdhowie :

Create protected methods that do the class-specific things, and call them from someMethod(). Class A will provide its implementation, and if a subclass needs to effectively remove that code, then it can override the protected method with an empty implementation.

Don't fight polymorphism; use it to your advantage.

Example:

public class A {
    protected void someOtherMethod() {
        // Do stuff specific to A here.
    }

    public void someMethod() {
        // Do some stuff

        someOtherMethod();

        // Do some more stuff
    }
}

public class B extends A {
    @Override
    protected void someOtherMethod() {
        // Empty implementation; B doesn't need to do this.
        // Or do stuff specific to B...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if object is a 'direct instance' of a class

Check if a object is a instance of a class (but not a instance of its subclass)

check in php if a object is a instance of a class but not a instance of a subclass

Calling a class method through an object instance

Check which child class is a parent class object

Check if some object is instance of some class in a list

Check if object is instance of any 'number' class?

JavaScript check if object is an instance of current class

Check if an object is instance of List of given class name

Check that a variable is an instance of a Class Object in WooCommerce

Check that object is not instance of any class in typescript

Jest and Typescript: Unable to check if object is instance of class

Calling the correct method for an object, when the object is an instance of a base class

Create child class object using parent class instance

How can I check if a generic class instance is actually an instance of a non-generic child?

Check if Object is an instance of a class from a list of classes and cast

How to check if an object is an instance of a template class in C++?

How can I check if a object is an instance of a specific class?

Can not access property from child class instance after calling super constructor in Typescript

Check ArrayList for instance of object

Check free object instance

Django model - interface design to avoid need of passing object in CHILD class calling method defined in PARENT class

Child class calling superclass constructor

Calling child functions with parent class

Calling class instance as a function in JavaScript

Calling New Instance of Class with DBContext

Calling an instance variable in another class

Calling a struct instance into a class? (Swift)

error calling class object?