aspose file tools
The moose likes Java in General and the fly likes Generic Composite Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generic Composite Interface" Watch "Generic Composite Interface" New topic
Author

Generic Composite Interface

Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
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


ph34r my 133t j4v4 h4><0r1ng sk177z
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
public class Org implements CompositeNode<Org> {
...
}


There is no emoticon for what I am feeling!
Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
Works great, thanks Jeff
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Generic Composite Interface
 
Similar Threads
JTree / DataModel Confusion
Getting the path of file selected in JTree item
Do I need synchronization here ?
method call from super/sub object ref.
Generic question