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

  • Home
  • SEARCH
  • 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 8904327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:06:07+00:00 2026-06-15T02:06:07+00:00

I have one class, let’s call it ClassA , and a bunch of subclasses

  • 0

I have one class, let’s call it ClassA, and a bunch of subclasses of it, and subsequent subclasses of those classes. ClassA, and every class below it takes a String at an argument in their constructor. I have a bunch of String objects that I need to ‘convert’ into subclasses of ClassA.

The current way I am doing it is with is isType(String) method that checks if the String is an instance of that subclass, but it seems like very bad programming technique to use a huge if-else or switch-case statement to find the correct type of ClassA that the String is. It there a common way to go down a sub-class structure, or is the way that I have been doing it okay?

The purpose of this is that I am making a scripting language (for a fun project) and I need to be able identify what something is. (Data type that is)

Example code:

public static boolean isType(String data) {
    data = data.trim();
    return edu.ata.script.data.Boolean.isType(data)
            || edu.ata.script.data.Integer.isType(data)
            || edu.ata.script.data.Double.isType(data)
            || DATA_STORAGE.contains(data)
            || ReturningMethod.isType(data)
            || edu.ata.script.data.String.isType(data);
}

public static Data get(String data) {
    data = data.trim();
    /*:)*/ if (edu.ata.script.data.Boolean.isType(data)) {
        return edu.ata.script.data.Boolean.get(data);
    } else if (edu.ata.script.data.Integer.isType(data)) {
        return edu.ata.script.data.Integer.get(data);
    } else if (edu.ata.script.data.Double.isType(data)) {
        return edu.ata.script.data.Double.get(data);
    } else if (DATA_STORAGE.contains(data)) {
        return (Data) DATA_STORAGE.get(data);
    } else if (ReturningMethod.isType(data)) {
        return ReturningMethods.getMethodValue(data);
    } else if (edu.ata.script.data.String.isType(data)) {
        // Last option because everything is accepted.
        return edu.ata.script.data.String.get(data);
    } else {
        throw new RuntimeException("Could not parse data - " + data);
    }
}
  • 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-06-15T02:06:08+00:00Added an answer on June 15, 2026 at 2:06 am

    I was indeed understanding something totally different, so I’m re-editing this answer.

    I suspect that you’re working in some sort of importation script or something and that the edu.ata.script.data.* are the ClassB, ClassC, etc that you were writing and they know how to convert stuff from one format to another… (This is just guessing)

    My suggestion to get rid of all the if then else in the get(String data) method is that you implement a chain of responsability. That way you can have a chain that registers the several edu.ata.script.data.* in a List and then you iterate through that list until one of them returns true for the isType(string) knowing which subclass you want to use to process the string.

    To remove the list of ORs in the isType(String data) method is a bit trickier and probably this is a bit over enginnering, but you can do the following:

    1. Look in your classpath for classes that are assignableFrom your class

    2. Then when you would retrieve all the classes that were assignable by your own you would have to invoke via reflection the isType of each one to know the result

      I know you might find yourself a bit lost regarding the 1st point, best way is to look into ResolverUtil class from Stripes Framework where they do that in the method loadImplementationsFromContextClassloader

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one abstract class -let's say myBase. And I want all the classes
I have one abstract class and two implementations. Let's call them ParentAbstract , ParentA
Ok, I have one class (let's call it: MenuBarClass) that contain multiple Menu and
Let's say I have one class Foo that has a bunch of logic in
Let's say I have two models, Classes and People. A Class might have one
Let we have one abstract class: classdef ACalculation < handle methods (Abstract) [result] =
Let's say we have a class that embed another parametric one, that has to
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I have one abstract class and many child classes. In child classes are from
I have a class (let's call it myClass ) that implements both __hash__ and

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.