• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing specific type methods from a generic abstract one

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day all, I have been digging around the forums and have done some google-fu but I can't seem to wrap my head around this problem.
Any help, advice, pointing to a direction is most welcome.









Now I would like to implement a receiver for a specific type of package declared in the code elsewhere, say "Vegetable"





The error says,
"incompatible types: Vegetable cannot be converted to elsewhere.Vegetable where Vegetable is a type-variable:
Vegetable extends Object declared in method <Vegetable> process(Package<Vegetable>)"

I did read about type erasure things in the net, but I did specify the method to only receive Packages with Vegetables so
why the inability to know that getting the Package's content will always return a Vegetable?

The IDE also hints at casting but then the error remains, also, what use do I have for having generics in the Packages when
I'm just going to cast it anyway, might as well make Packages receive any Object then cast it when I need to.


Any and all help, advice, or clue is appreciated and most welcome.
Thank you for the time and consideration.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with this line in your class VegetableReceiver:

What you are doing here, which makes this very confusing, is that you define a type parameter named Vegetable - it has the same name as your class Vegetable. So, Vegetable in this method does not mean the concrete class Vegetable, it's a type parameter, that can be substituted with any concrete type when we call the method.

Let's rename the type parameter. This won't change the meaning of the method, but makes it clear what is happening.

This is exactly the same as your method, except that we have now named the type parameter V instead of Vegetable.


How to solve this:

1. Put the type parameter on class Receiver instead of on the method in the class:

2. Make VegetableReceiver extend Receiver<Vegetable>, and don't define a new type parameter on class VegetableReceiver or on the method:

The first step is necessary otherwise you will not be able to override the method for a specific type in a subclass of Receiver - the only thing you could do is override the method with a type argument, and that's not what you want in this case.

(There's also a bug in your class Receiver - you cannot name the argument variable to the method "package", because that is a keyword in Java).
 
T James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. I get it now. I have been tackling this problem for about a week now. I am blown away. You can have my Cookie Jar. Take them all.

However, I've been thinking, why CAN'T we specify a type in the method itself, rather than on the class that has it?
I mean, is there any solid reasoning why I can't specify a specific type on the method, kinda makes it easier to understand though (with scope and all that).

Thank you very much for your insight and fix!
Also for calling out the package thing, sorry I made it for example purposes, should've used grocery bags or something...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic