Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Confusing interface Date: Fri, 7 Jul 2017 09:45:44 -0400 Organization: A noiseless patient Spider Lines: 72 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 7 Jul 2017 13:42:01 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="efaa7c448619693266938c9656a689a7"; logging-data="490"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VWcLZ6N+PD1hhe2t8ooOe" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: Content-Language: en-US Cancel-Lock: sha1:La/KN19cjA4d9I6xduXd6n2wUV8= Xref: csiph.com comp.lang.java.programmer:36811 On 7/7/2017 9:10 AM, Eric Douglas wrote: > Oracle defines this > public interface TreeNode > > which contains this > TreeNode getParent(); > > Then they define this > public interface MutableTreeNode extends TreeNode > > which contains this > void setParent(MutableTreeNode newParent); > > Then they define this > public class DefaultMutableTreeNode implements Cloneable, > MutableTreeNode, Serializable > > which contains this > @Transient > public void setParent(MutableTreeNode newParent) { > > public TreeNode getParent() { > > Now I define > public class MyCustomTreeNode extends DefaultMutableTreeNode > > and Eclipse allows > @Override > public MyCustomTreeNode getParent() > > but I must keep the signature > public void setParent(MutableTreeNode newParent) > > If I put @Override on the set and declare the parameter as MyCustomTreeNode, it says must override or implement a supertype method. > If the get and set are both using interface parameters, and my custom class is simply extending a class which implements one interface which extends the other, why is one allowed and not the other? Because `setParent(MyCustomTreeNode newParent)' does not override the setParent() method of the superclass. You can write the method with the signature you like, but it's not an override. Consider: MyCustomTreeNode parent = ...; MyCustomTreeNode child = ...; child.setParent(parent); So far, all is well and behaves as you intend. Next, however: DefaultMutableTreeNode kidnapper = ...; child.setParent(kidnapper); Question: Which setParent() method runs, the one in your class or the one in the superclass? Right you are, it's the superclass' method -- which means your class has not overridden it. I imagine many people encounter this same confusion -- I confess that I did, too, many years ago. I implemented class MyMap extends AbstractMap ... and provided the method Wotsit get(Thing key) To my surprise, some calls to get() went directly to the get() of AbstractMap and not to my code! I had made the same mistake then that you are making now. (My error was before @Override came onto the scene, so the compiler didn't alert me. Be thankful that the tools have improved so you're faced with a compile error instead of with a baffling mystery.) -- esosman@comcast-dot-net.invalid Twelve hundred ninety-three days to go.