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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:31:20+00:00 2026-05-21T05:31:20+00:00

I am defining the following class: public class Foo<T> { T _value; public T

  • 0

I am defining the following class:

public class Foo<T> {
    T _value;

    public T getValue() { 
        return T;

    public void setValue(T value) {
        _value = value;
    }
}

T should only be of type int, char, or double.

I want to be able to reference a collection of these, such as:

interface IBar {
    Collection<Foo> getAllFoo();
}

Obviously, this doesn’t work since I’d need to specify the type T with this reference. So, I need to define some abstract base class that I can use instead.

The question is, how can I enforce that all derived types implement the getValue() and setValue() functions? I know I can define them in the abstract base as abstract methods returning / accepting an Object, but that doesn’t provide type safety on the 3 accepted primitives.

Is there a better way to define a collection of these, and is there a way to enforce T being either int, char, or double?

  • 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-21T05:31:21+00:00Added an answer on May 21, 2026 at 5:31 am

    You’re better off creating concrete implementations for each type you care about.

    interface Foo<T>{
        public T getValue();
        public void setValue(T value);
    }
    
    class IntFoo implements Foo<Integer>{...}
    class CharFoo implements Foo<Character>{...}
    class DoubleFoo implements Foo<Double>{...}
    
    interface Bar{
        Collection<Foo<?>> getAllFoo();
    }
    

    The ? allows the collection to contain any type of Foo object. You still lose the generic type when you reference a Foo<?>, but there’s no good way around that.

    Other than having the common base class, there really is no advantage of doing this. If you had some common ground each Foo could resolve to, then I would do this:

    abstract class Foo<T>{
        T value;
    
        T getValue(){
            return value;
        }
    
        void setValue(T value){
            this.value=value;
        }
    
        // return whatever is appropriate for all implementations
        abstract Object baz(); 
    }
    
    class IntFoo implements Foo<Integer>{
        Object baz(){
            return null // something useful;
        }
    }
    
    Collection<Foo<?>> foos = bar.getAllFoo();
    for(Foo<?> foo:foos){
        foo.baz();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After defining the following : abstract class A { type T def print(p: T)
Given the following two ways of defining a class method in Ruby: class Foo
Why does the following throw a compiler error: class A { public: int f()
Say I have the following POCO classes: public class AuditableModel { public int ID
I'm defining an unordered_map in C++ like the following: unordered_map<CustomClass, int, CustomClassHash, CustomClassEq> myMap;
Suppose we have following two classes: class Temp{ public: char a; char b; };
Let's say we have the following piece of code: public class Event { }
I have the following hierarchy of classes class classOne { virtual void abstractMethod() =
In defining my Element class I get the following error: no matching function for
I am defining a static factory method with: @XmlType(factoryClass=DummyFactory.class, factoryMethod=createNew) public abstract MyClass() {

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.