• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Composite Interface  RSS feed

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
public class Org implements CompositeNode<Org> {
...
}
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works great, thanks Jeff
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!