• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics error.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm implementing tree class for supporting various tree operations, and for storing data, I'm using TreeNode class. The structure is as follows:



I tried changing the above TreeNode class to accept only bounded parameter T using the following code to solve ERROR 1:



but error still exists. Also for second error, I already have a restriction on insert method to accept only values which are comparable, hence, I don't really understand the source of this error.
Is there any way I can eliminate these errors, without having to place a restriction on the class input to accept only Comparable values. I just want the insert method to have that restriction as it uses compareTo method.
Also, I won't mind any suggestions for a good generics tutorial, I have already read from Oracle documentation, but it still appears pretty much confusing to me. Thanks.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look here for some wonderful tutorials on Generics!

http://www.angelikalanger.com/GenericsFAQ/FAQSections/Features.html
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you try implementing the Comparable on your TreeNode?



 
Brendon McCullum
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe, after a little bit more googling, I was able to solve both problems. But a code review for below code would certainly help:



Also, thanks for the link. I'll check it out.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brendon, here for the nitpicking again :P

- You should make your class declaration class Tree<T extends Comparable<? super T>>. The reason for this is that if you have a class that knows how to compare its instances, you can also create Trees of its subclasses. Let's say you have a class Fruit implements Comparable<Fruit>, and a class Pear extends Fruit. With your old class header, you won't be able to create a Tree<Pear>, because Pear does not extend Comparable<Pear>.

- Your root node should have the same type parameter as your tree: private TreeNode<T>.

- A TreeNode intrinsically belongs to a specific Tree. Don't make the class static. That way, you can even navigate back to the root from any node with Tree.this.root.

- The findHeight() methods have a type parameter. They don't need one, because they're not concerned with any element types to find the height of a tree.

- Your recursive findHeight() method doesn't use context (it only operates on the passed argument) so it can be static.

- The insert() and insertNode() methods don't need type parameters. They use the same type arguments as the Tree class. This will likely solve your unchecked problems (in combination with using a properly typed root).

- Your Tree class is not final, but you do define a bunch of final methods on it. It looks like you are designing for inheritance. You should consider overriding clone() to facilitate sub-classes that might want to implement Cloneable, you should consider implementing equals() and hashCode() and make them final, or specify what they do in a method stub with a contract. You're also not allowing sub-classes the opportunity to define more efficient implementations of findHeight() or insertNode().
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I wouldn't bind every Tree to a Comparable type. Instead, I would do something like this:
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic