• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generics driving me crazy, for the fourth time

 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got the following interface and implementation:





Sure looks good to my eyes. But the compiler hates it. Specifically, I get:



Sure looks like "found" and "required" are the same to me. Any ideas what I'm doing wrong?
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 12 of the interface you declare a type parameter T, and unfortunately you give it the same name as the type parameter in the declaration of the interface. So in a more readable form it's this:

Perhaps you might want this instead:

That looks very much like what I ended up with when I tried to write a generic tree implementation.
 
Pat Farrell
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:At line 12 of the interface you declare a type parameter T, and unfortunately you give it the same name as the type parameter in the declaration of the interface. So in a more readable form it's this:



I want the T to be the same one as the Type T of the thing, so we can have a Node<String> or a Node<Orange>

What then defines type "U"? I want it to be a Node<T> but I don't seem to know how to express that.

Paul Clapham wrote:Perhaps you might want this instead:



This won't compile for me at all. Tons of errors, nearly every character in the generic part.

etc.....

Paul Clapham wrote:That looks very much like what I ended up with when I tried to write a generic tree implementation.



Did you succeed? or is this an hopeless effort? Would seem to be a fundamental data structure, should be easy to do. Famous last words.
 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what is it you want? If you want the type of the parent to depend on whatever the type of the object you're passing to the setParent() method is, you can't do this in Java.

If you just want to keep a reference to the parent, and it doesn't matter what the type of the parent is, as long as it extends T, then your code should look like this:
 
Pat Farrell
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Well, what is it you want? If you want the type of the parent to depend on whatever the type of the object you're passing to the setParent() method is, you can't do this in Java.


I'll try your code fragment.

I want the parent of a node of orange to be a Node<Orange>

I thought that if you define a tree of Oranges, you can have iterators that return Node<Orange> etc.

Its not that the invocation of the setParent() varies, when you define a bunch of Node<Orange>'s, then type safety would say you can only call
setParent() with a value that is a Node<Orange> But it doesn't seem to work,or my understanding is all out of sync with generics.
 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you want the parent of a tree of oranges to be a node of oranges, then you simply need a method signature:
Unrelated: Why is the class called AbstractNodeTree and not just AbstractNode?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:I want the parent of a node of orange to be a Node<Orange>

I thought that if you define a tree of Oranges, you can have iterators that return Node<Orange> etc.


But unfortunately, that's not what you said with
<T extends Node<T>> void setParent(Node<T> parent );
mainly, as Stephan says, because you used the same generic variable (T) in the declaration.

Its not that the invocation of the setParent() varies, when you define a bunch of Node<Orange>'s, then type safety would say you can only call
setParent() with a value that is a Node<Orange> But it doesn't seem to work,or my understanding is all out of sync with generics.


If the only thing you wanted was a Node of the same type, you could have simply written:
void setParent(Node<T> parent);

Winston
 
Pat Farrell
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve,

I swear that I tried that, and it didn't work.
But I've been beating my head against this for half a day, and I may be crazy.

Its called FooTree because when I wrote it, I thought that I needed both a Node and a NodeTree, and that I'd be subclassing a base AbstractNodeTree. But it looks like I don't need all that.

Current plan is that the root of any tree is just a Node<Foo> with a null parent.

99% of the time, Java Generics work great for me, but that other 1 percent, make my brain explode.

 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:I thought that I needed both a Node and a NodeTree



Haha, yeah that sounds familiar
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:9(% of the time, Java Generics work great for me, but that other 1 percent, make my brain explode.


Wait until you get onto stuff like:
List<? extends Comparable<? super T>> list ...

Winston
 
Replace the word "snake" with "danger noodle" in all tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic