Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36811

Re: Confusing interface

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Confusing interface
Date 2017-07-07 09:45 -0400
Organization A noiseless patient Spider
Message-ID <ojo339$fa$1@dont-email.me> (permalink)
References <cd610d1e-ecc4-443b-b346-eb8847b22b74@googlegroups.com>

Show all headers | View raw


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<Thing,Wotsit>

... 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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 06:10 -0700
  Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 09:45 -0400
    Re: Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 07:37 -0700
      Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 11:26 -0400
        Re: Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 11:24 -0700
          Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 14:50 -0400
            Re: Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 12:00 -0700
              Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 15:27 -0400
                Re: Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 12:47 -0700
                Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 16:25 -0400
                Re: Confusing interface Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 13:39 -0700
                Re: Confusing interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 17:43 -0400

csiph-web