• 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

Generic Composite Interface

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

Quick question on my generic composite interface.

I have the following interface



And a concrete class



The compiler complains that I have not implemented the method addChild from the CompositeNode interface - if I change the argument type of the addChild method in the implementation to CompositeNode it compiles OK (after changing the list).

The getChild method works OK so I am not understanding why the addChild is no good.

Can anyone help?

Thanks in advance
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Org implements CompositeNode<Org> {
...
}
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works great, thanks Jeff
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also:

[Rick]: The getChild method works OK so I am not understanding why the addChild is no good.

When overriding or implementing a method, you can always (as of JDK 5) provide a more specific return type than the original method declaration did. That is, if the original return type was a reference type, the overriding return type may be a subclass or subinterface of the original. This is true regardless of whether the class is generic or not.
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim, that I didn't know. That's two things I have learned today... I can take tomorrow off

I am trying to think of a situation where returning a subtype in an overridden method would be useful - mostly overridden classes will be called polymorphically so the client would normally not be aware of the more specific subtype.

Can you think of an example of where this may be useful to aid my understanding of it's use?

Thanks

Originally posted by Jim Yingst:
Also:

[Rick]: The getChild method works OK so I am not understanding why the addChild is no good.

When overriding or implementing a method, you can always (as of JDK 5) provide a more specific return type than the original method declaration did. That is, if the original return type was a reference type, the overriding return type may be a subclass or subinterface of the original. This is true regardless of whether the class is generic or not.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic