Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: interfaces and inheritance



Todd O'Bryan wrote:

>public interface TreeNode {
>    TreeNode getParent();
>    List getChildren();
>    Object getValue();
>}
>public class TypeATreeNode implements TreeNode {
>    public TypeATreeNode getParent() {
>        // code
>    }

Your implementation of getParent() is returning a narrower type than the
interface's getParent().  So either you're not implementing a required
method, or the return-types are incompatible.  I.e. not every TreeNode is a
TypeATreeNode.

Declare the implementations to return TreeNode.


>If so, why not?

Because the return-types won't match.

Read the sections in the Java Language Specification about return-types,
and also see the sections on implicit widening and non-implicit narrowing.

  -- GG


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.