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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:14:42+00:00 2026-06-03T06:14:42+00:00

Say, i have a generic type as below public class GenericType<T> { private T

  • 0

Say, i have a generic type as below

public class GenericType<T> {
   private T someVar;

   public void setVar(T var) { this.someVar = var; }

   //Rest of the code
}

I want to allow it to take only specific types(String/Integer/Double). I know about bounded wildcards but they don’t help me here. In setVar(), I can check the instanceof and throw an Exception if type is not Integer/String etc. Is this the best way to do it?

I have the same problem when doing operations on this type. Depending on the type, I want to do different operations. Inheritance and bounded wildcards seem like the way to go in general for this kind of problem but these are primitive wrappers.

  • 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-03T06:14:43+00:00Added an answer on June 3, 2026 at 6:14 am

    Using Inheritance:

    Parent.java

    public abstract class Parent<T> {
        public abstract void display(T t);
    }
    

    ChildString.java

    public class ChildString extends Parent<String> {
    
        @Override
        public void display(String t) {
            // Do something here...
        }
    
    }
    

    ChildInteger.java

    public class ChildInteger extends Parent<Integer> {
    
        @Override
        public void display(Integer t) {
            // Do something here...
        }
    
    }
    

    ChildDouble.java

    public class ChildDouble extends Parent<Double> {
    
        @Override
        public void display(Double t) {
            // Do something here...
        }
    
    }
    

    And access the class child rather than you directly access the parent class.

    Update

    Here another example:

    GenericType.java

    public class GenericType {
    
        public void display(Object t) {
            String msg;
            if(t instanceof String) {
                msg = "String";
            } else if (t instanceof Integer) {
                msg = "Integer";
            } else if (t instanceof Double) {
                msg = "Double";
            } else {
                msg = "Another Object";
            }
    
            System.out.println(msg);
        }
    }
    

    SpecificGeneric.java

    public class SpecificGeneric {
    
        public static void main(String[] args) {
            GenericType basicType = new GenericType();
    
            basicType.display(new String());
            basicType.display(new Integer(1));
            basicType.display(new Double(0.1));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a generic method with multiple type constraints, this this: public static
Lets say i have this class: class Test123<T> where T : struct { public
Lets say I have a generic class: class Foo { // protected Type t;
I have a generic list of type Element , for example. public class Element
Say I have the following generic class: public class Foo<T extends Bar> { //
Let's say I have a generic Object class, and a generic List class. I
Say that i have a generic dictionary with data like this (I hope the
Say I have a couple of basic objects like so: [Serializable] public class Base
Let's say I have two generic lists of the same type. How do I

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.