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


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

Re: Confusing interface

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Confusing interface
Date 2017-07-07 11:26 -0400
Organization A noiseless patient Spider
Message-ID <ojo90v$l5f$1@dont-email.me> (permalink)
References <cd610d1e-ecc4-443b-b346-eb8847b22b74@googlegroups.com> <ojo339$fa$1@dont-email.me> <0175016c-5ab9-4867-a9d1-7c43907e4427@googlegroups.com>

Show all headers | View raw


On 7/7/2017 10:37 AM, Eric Douglas wrote:
> On Friday, July 7, 2017 at 9:46:10 AM UTC-4, Eric Sosman wrote:
>>       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.
>>
> 
> but the setParent method in DefaultMutableTreeNode accepts a MutableTreeNode, not a DefaultMutableTreeNode.  So I would think if you override that in MyCustomTreeNode to accept a MyCustomTreeNode, then if child is an instance of MyCustomTreeNode, it should throw an error on setParent if the parameter is not an instance of MyCustomTreeNode.

     Yes, DefaultMutableTreeNode::setParent accepts a MutableTreeNode.
*Any* MutableTreeNode, including a DefaultMutableTreeNode or a
MyCustomTreeNode or a FoundItSomewhereOnStackOverflowTreeNode or ...
Your non-overriding method accepts only a MyCustomTreeNode, not
any of the others.

     Here's a pared-down version that avoids the complications of
interfaces and Swing; try it and see if you understand what's afoot:

	class Super {
	    void doIt(Super s) {
	        System.out.println("Super::doIt");
	    }
	}

	class Sub extends Super {
	    void doIt(Sub s) {
	        System.out.println("Sub::doIt");
	    }
	}

	class Driver {
	    public static void main(String[] unused) {
	        Super sup = new Super();
	        sup.doIt(sup);  // no surprises yet
	        Sub sub = new Sub();
	        sub.doIt(sub);  // no surprises yet
	        sub.doIt(sup);  // surprised?
	        sub.doIt((Super) sub);  // how about now?
	    }
	}

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