Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:54:43+00:00 2026-05-23T22:54:43+00:00

I have 2 classes. Let’s call them class A and class B. Class A

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:54:44+00:00Added an answer on May 23, 2026 at 10:54 pm

    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...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that I am testing (let's call them ClassA and ClassB).
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have 3 classes, let's call them 1, 2, and 3. Class 1 extends
Let's say that I have classes like this: public class Parent { public int
Let's say I have two classes that look like this: public class ByteFilter {
I have two classes (let's call them A and B ), both inheriting from
let's say I have 2 classes like this: public class Foo { [Required] public
So let's say I have two classes that inherit a base class that has
Let's assume I have three classes that are subclasses of a base class: public
Let's say I have a classes, like that: class A { public static int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.