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 8315573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:09:45+00:00 2026-06-08T21:09:45+00:00

I have one supertype defined as: public abstract class AType<T> { …. private T

  • 0

I have one supertype defined as:

public abstract class AType<T> {
        ....

    private T value;
    private T mask;

    public T getValue() {
        if (isMasking())
            return null;

        return this.value;
    }

    public void setValue(T value) {
        if (value == null)
            throw new IllegalArgumentException("Value is mandatory.");

        this.value = value;
    }

    protected T getMask() {
        if (!isMasking())
            return null;

        return this.mask;
    }

    protected void setMask(T mask) {
        if (mask == null)
            throw new IllegalArgumentException("Mask is mandatory.");

        this.setMasking(true);
        this.mask = mask;
    }
        ...
}

and few subtypes like:

public class SpecType extends AType<Integer> {
    ...
}

these sub types specifies the unknown parameter…. i have more f.e. IPv4, Long, and so on

now i need to somehow in runtime do a dynamic cast…
i have these classes defined in enum like this:

public enum Type {
    SOME_TYPE(new TypeID(0, (short) 0), OFMU16.class,
            new Instantiable<AType<?>>() {
                @Override
                public SpecType instantiate() {
                    return new SpecType(new OFMatchTypeIdentifier(0, (short) 0));
                }
            }),...;

    ...

    public Class<? extends AType<?>> toClass() {
    return this.clazz;
}

    ...

}

I want do something like:

AType<?> type = SOME_TYPE.newInstance();    //this works

SOME_TYPE.toClass().cast(type).setValue(10);    //this don't work

so I have to do it statically:

((SpecType) type).setValue(10);

Everything would be OK, but the user of this module will not want to look in enum and cast manually every time. This will probably make mistakes and spend a lot of time with debugging :/….

My question is how can I refactor this or how do I define structure of inheritance to allow user to cast dynamically? Is it possible?

Edit:
I am parsing packets from network. There is a lot types which differs in Vendor Type identifier and type of Value/Mask – these fields are all constant for every this combination, so i has defined it as enum constants. F.e. 20 have different only TypeID but same VendorID and all of them can be represented as Integer, next 10 differ in VendorID And TypeID but all of them can be represented as Short and so on.

  • 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-08T21:09:46+00:00Added an answer on June 8, 2026 at 9:09 pm

    It’s still not clear why you should have to cast at all. As soon as SOME_TYPE is written into your sourcecode OR the type of set setValue method is hardcoded (in your example int or Integer) you don’t need runtime checking – you need compile time checking.

    So I suppose the following snippet is how your API users should code:

    public class TypeTest {
        public static void main(String[] args) {
            AType<Integer> type0 = Types.SOME_TYPE_0.instantiate();
            type0.setValue(10);
    
            AType<String> type1 = Types.SOME_TYPE_1.instantiate();
            type1.setValue("foo");
        }
    }
    

    I have stripped down your example to the bare minimum which is required to understand the Generics part:

    abstract class AType<T> {
        private T value;
    
        // standard getter/setter
        public T getValue() { return this.value; }
        public void setValue(T value) { this.value = value; }
    }
    
    class SpecTypeInt extends AType<Integer> {
    }
    
    class SpecTypeString extends AType<String> {
    }
    
    interface Instantiable<T> {
        T instantiate();
    }
    

    The key part is: Don’t use an enum, because an enum cannot have type parameters. You can use a plain interface instead like the next snippet. Each reference in the interface points to a factory. Each factory knows a) the abstract type and b) the concrete type. To make Generics happy you have to glue a) and b) together with ? extends X.

    interface Types {
        Instantiable<? extends AType<Integer>> SOME_TYPE_0 = new Instantiable<SpecTypeInt>() {
            @Override
            public SpecTypeInt instantiate() {
                return new SpecTypeInt();
            }
        };
    
        Instantiable<? extends AType<String>> SOME_TYPE_1 = new Instantiable<SpecTypeString>() {
            @Override
            public SpecTypeString instantiate() {
                return new SpecTypeString();
            }
        }  ;
    }
    

    Cleanup: Must your user look into the interface: Yes, he must in any case, because he must know which is the appropriate type for setValue 1. NO solution can circumvent this. Although Eclipse might help you and your users a little bit: In main just type Types.SOME_TYPE_1.instantiate(); then go to the start of the line, hit Ctrl2 + L (“Assign to loccal variable”) and Eclipse replaces the AType<String> instantiate = part for you.


    1If your users don’t know the right type for the setValue method, then you are asking the wrong question. In that case you should have asked something like “How to design a Generic safe conversion facility?”.

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

Sidebar

Related Questions

One can say a type parameter T must have a specific supertype S_1: class
I have a custom event class public class FFTDrawEvent extends Event { public static
I have 3 tables, one which represents a supertype, with an ID column. Two
I have one input inference in Virtuoso Open Source, that was defined from goodrelations
I have one class that shows me a list of Items. Right now I
have one time consuming step that flattens a bunch of files. basically i'd like
I have one XML request which I need to modify (to XML) and then
I have one user (only one, all the others are fine) trying to update
I have one repo hosted at https://github.com/aikiframework/json . On my local copy, I added
I have one drop down list in my page, which contains two options. What

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.