1

This was a hard problem to try and search for as I've seen no other specific issues like this, so I apologize if there turns out to be a duplicate thread I haven't found. This is a programming problem that is not related to homework in a class, that I am having issues implementing exactly according to spec.

If I want to create a

Function<T>

generic abstract class, whose only abstract method is

Apply<T>(T input1, input2)

and then extend into other derived function classes like an AddTwoNumbers class, to which Apply would return the sum of those input1 and input2 of type T (in practice this should be Integer or Double), how can I implement this?

I run into a problem when I first have

public abstract class Function<T1 extends Number> {
    public abstract T1 apply(T1 input1, T1 input2);
}
public class AddTwoNumbers<T1 extends Number> extends Function<T1> {
    public T1 apply(T1 input1, T1 input2) {
        return input1.intValue() + input2.intValue(); //Error, needs to return type T1, not int or even Integer()
    }
}

It is required that Function be generic that allows types, but inpractice it can be expected that AddTwoNumbers will be using Integers exclusively for the sake of this problem. Is there a way I can write the apply() method in AddTwoNumbers, such that

  1. AddTwoNumbers implements the abstract methods inherited from Function exactly
  2. Somehow add the two T1 values and return a T1 value properly?

The fact that I can't safely cast a T1 to an Integer, and also retain the overriding of Apply is the troublesome part.

This is generalization of the issue I was looking into.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user242083
  • 13
  • 2
  • You may be looking for [`JScience`](http://stackoverflow.com/tags/jscience/info), for [example](http://stackoverflow.com/a/8285147/230513). – trashgod Oct 04 '13 at 03:17

2 Answers2

1
public class AddTwoIntegers extends Function< Integer > {
...
}
Judge Mental
  • 5,209
  • 17
  • 22
  • +1 I think this is the correct answer. But a static typed method would be a more interesting question – Bohemian Oct 04 '13 at 03:58
  • hey you were right! Thank you so much Judge Mental! I tried doing it AddTwoIntegers and didn't work but the syntax you specified is correct. Also, I also think Apply() should be a static method but java doesn't allow Abstract Static methods. Some people think it makes sense but I wish they would allow both. – user242083 Oct 04 '13 at 04:13
  • Why would `apply` make any sense as a static method? There's no functional object instance any more and no concept of overriding in that case. – Judge Mental Oct 04 '13 at 04:15
  • I probably just don't understand the idea that this exercise is trying to get across -- a functional object instance. I'm used to thinking of it in terms of, if the function isn't using any data members of the class then might as well make it static. – user242083 Oct 04 '13 at 04:29
  • Check out the Flyweight design pattern. It would make sense to share a single, global (static?) instance of this class throughout your program, as you point out. But in this model there has to be at least one instance to represent each different function that you want to use. – Judge Mental Oct 04 '13 at 04:37
  • Also, as a matter of SO etiquette, one normally clicks the checkmark next to an answer if it is correct. – Judge Mental Oct 04 '13 at 04:48
  • Thanks Judge Mental, as I said I'm new so I appreciate the tip. Checked! – user242083 Oct 04 '13 at 04:51
0

What you're asking for unfortunately isn't possible within the Java specification. There is no abstraction layer which accounts for both primitive ints and primitive doubles, or in fact any combination of primitives, so you are forced to create two classes for this.

There are ways of doing this, however, which are much better than writing an AddTwoIntegers.java file AND an AddTwoDoubles.java file.

Option 1: Create an AdditionFunctions class, and give it two static members: INTS and DOUBLES. You can do this using anonymous classes easily.

Option 2: Use a template, if you can incorporate the generation step into your build process. Trove does this to great effect

torquestomp
  • 3,304
  • 19
  • 26