2

I have an abstract generic method in a abstract class:

/**
 * @param <Value>
 */
public abstract class TestAbstract<Value> {

    public abstract <Value> void test(Value value);

}    

Now I extend from this class and implement the abstract generic Method - this is my attempt, but this is obviously wrong.

public class Impl extends TestAbstract<Integer> {

    @Override
    public <Integer> void test(Integer value) {

        // value is instanceOf as java.lang.Object and not java.lang.Integer !

    }

}

Within test() value is treated as Object and not as Integer...

How can I override this Method, so that I am forced to use the 'integer'-Class?

Thanks!

9
  • 1
    Is Value an actual class in your project? Commented Jan 10, 2015 at 11:45
  • No. I want value to be a parameter for this abstract class... Commented Jan 10, 2015 at 11:47
  • @peddn: It's a bit unclear what you mean with "treated as Object", can you be more specific? Commented Jan 10, 2015 at 11:48
  • 1
    value is a instance of 'Object' and not a instance of 'Integer'. Commented Jan 10, 2015 at 11:49
  • if you mean int, that is not possible with generics. Commented Jan 10, 2015 at 11:49

3 Answers 3

6

You are replacing the type parameter with a concrete type. You probably intended to do something like this:

public abstract class TestAbstract<V> {
    public abstract void test(V value);
}    

(note that, by convention, type parameters should be a single uppercase letter, if possible)

Now, when you create a concrete implementation of this class, you can substitute a concrete type for this type parameter:

public class Impl extends TestAbstract<Integer> {

    @Override
    public void test(Integer value) {
        // Here, "value" is an Integer
    }
}

In this case, you don't need a type parameter for the method itself, because it is defined in the surrounding context (namely, in the class).

Sign up to request clarification or add additional context in comments.

5 Comments

@Marco13 - You wrote that "you don't need a type parameter for the method itself." Can you do this at the method level instead of at the class level, or must this be done at the class level?
@risingTide It's possible at method level, see docs.oracle.com/javase/tutorial/java/generics/methods.html
@Marco13 Sorry, I was unclear. I know that it is possible overall, but I wondered what a method level implementation would look like in specific example above. I see that your response has taken it to a class level implemenation; is there a method level implementation for this particular issue?
@risingTide At least, you can't fix the type in a subclass in this case. For details, consider asking about your particular use case in a separate question (if a similar question was not asked before)
@Marco13 Thanks, that's enough related to this question. If I would post my own example it would be exactly the same code as above anyway, which I why I asked here; I didn't want to duplicate.
1

test in Impl is not intended to be a generic method. It should only apply to the class Integer. Therefore you simply need to change

public <Integer> void test(Integer value)

to

public void test(Integer value)

1 Comment

Indeed, here it is seen as a generic method, well spotted! Perhaps you should provide a link to generic methods to make it more clear...
1

Change the abstract to this:

public abstract class SuperClass<T> {
    public abstract void test(T value);
}

And the subclass to:

public class SubClass extends SuperClass<Integer> {
    public void test(Integer value) {
    }
}

And it works now, as there is no longer a parameter hiding issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.