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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:42+00:00 2026-05-26T21:09:42+00:00

I have an operator class that has some classes and methods in it that

  • 0

I have an operator class that has some classes and methods in it that deals with mathematical operations. The operator class is generic and extends Number so my operations can be with ints, doubles, floats, etc.

public class OperatorClass<T extends Number>

In this class there is an abstract class that provides a method in which numbers are operated upon using the calculate method

abstract class Op<T extends Number> {
    abstract T calculate(DataSet<? extends Number> ds);
}

There are multiple classes that extend Op like the AddOp for example:

class AddOp<T extends Number> extends Op<T>{
    public T calculate(DataSet<? extends Number> ds) {
        return ds.getLeft() +  ds.getRight();
    }
}

And my DataSet is just a data class that looks like this:

public class DataSet<T extends Number> {
private T left;
private T right;

public DataSet(T left, T right) {
    this.left = left;
    this.right = right;
}

public T getLeft() {
    return left;
}
public T getRight() {
    return right;
}

}

The problem I am having is that the line ds.getLeft() + ds.getRight() is not compiling because the Number abstract class cannot do a + operator. How do i get my DataSet to be a type of Number (like a wildcard class) so i can do math operations on it.

Any help would be appreciated
Thanks

Edit

So a workaround to the problem is that I used some if statements and then got the correct primitive datatype like so

Number result = null;
if(ds.getLeft().getClass().equals(Integer.class))
     result = new Integer(ds.getLeft().intValue() + ds.getRight().intValue());
....
return (T) result;
  • 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-26T21:09:43+00:00Added an answer on May 26, 2026 at 9:09 pm

    You have to decide at what resolution you are going to add. In other words, are you calling left.getInteger() or left.getFloat()?

    There are a number of ways of solving this, but it really boils down to what you intend to do. If you want your numbers to truly remain objects, you cannot use the + plus operator because that doesn’t work on objects. You need a left.plus(right) style method call which returns a new number.

    If you don’t care about maintaining your numbers as objects during math exercises (perhaps they were just objects for the sake of collection storage or something) then you replace the public T getLeft() with public long getLeft() and rely on the internal number’s left.getLongValue() to do the conversion. Or you just expose the two Number objects and allow the “external” user to do the appropriate getXxxValue() calls.

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

Sidebar

Related Questions

I have a class that defined a user defined operator for a TCHAR*, like
In C++, can you have a templated operator on a class? Like so: class
I have a class that should hold a reference to some data, without owning
I'm writing a templated C++ generic container class that can optionally maintain its contents
I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<
I have a vector class that has it's Equals(object obj) method overridden so that
I have a class IDocument which serve as a interface for some classes. It
I have an abstract functor class that overloads operator() and derived objects that implement
I have a class Foo, which has a (simple) destructor. Some other class contains
I would like to add an operator to a class. I currently have a

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.