0

i have the following Problem. I have a Interface

public interface A<T extends B>
{
   T getObject();
}

And i have a class using this interface:

public void test()
{
  A<B> a;
  B b = a.getObject();
}

This causes an compile error cause the returned Object is B and not extending B. Is there any way i can declare variable b with ?

Thanks, Gertrude

  • Is there a reason you're using the raw type `A` instead of `A a`? – ernest_k Sep 30 '19 at 10:53
  • You're using a raw type `A`, don't do that. If you don't know `T`, then use `A>` instead. – Joachim Sauer Sep 30 '19 at 10:53
  • Start by reading https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it ... then, in case you still wonder what is going on ... read [mcve] and enhance your question accordingly. At this point in time, my guess is that you simply lack knowledge about java generics. – GhostCat Sep 30 '19 at 10:53
  • sry i forgot about A, but my question still exists ;) – user3922628 Sep 30 '19 at 10:57
  • 2
    if you call a.getObject() without intializing a, then you get a compiler error that `The local variable a may not have been initialized`. – Sujay Mohan Sep 30 '19 at 10:58
  • @SujayMohan this is an example code and not the problem i was asking about – user3922628 Sep 30 '19 at 12:21
  • 1
    please go through this link - https://stackoverflow.com/help/minimal-reproducible-example. As per the code shared by you does not cause the error mentioned by you. Unless you provide the code for the implementation of getObject() is provided and implementation of Object a is provided it is difficult to provide an answer to question with the code provided. – Sujay Mohan Sep 30 '19 at 12:33
  • @user3922628: sample code is fine, usually, but this one doesn't demonstrate the issue you are trying to solve, so it's pointless here. – Joachim Sauer Sep 30 '19 at 14:48

0 Answers0