Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36828
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Confusing interface |
| Date | 2017-07-07 16:25 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ojoqgk$h33$1@dont-email.me> (permalink) |
| References | (4 earlier) <c30448c3-8f2b-4eba-80c4-c395566dd970@googlegroups.com> <ojokuh$tqh$1@dont-email.me> <ad49ff16-7feb-46d7-82df-d13b22d69a90@googlegroups.com> <ojon3m$5ff$1@dont-email.me> <74f526e3-2f48-4e72-a52c-f3b998f61997@googlegroups.com> |
On 7/7/2017 3:47 PM, Eric Douglas wrote:
> On Friday, July 7, 2017 at 3:27:41 PM UTC-4, Eric Sosman wrote:
>> But this still has nothing to do with your original question,
>> which was (boiled down to essentials): "If a superclass has a method
>> that takes a Thing parameter, why can't I override it with a method
>> taking a SubclassOfThing parameter?" But you're now trying to pose
>> the same question about clone() -- and since clone() takes zero count
>> them zero parameters, the question has no meaning.
>>
>
> Right then, same topic different question.
> So
> 1) You can always override the return value of a method with a subclass of that value.
Right. If the overridden method returns a Thing, the overriding
method can return a SubclassOfThing. Why? Because a SubclassOfThing
"is a" Thing, so the overriding method still follows the overridden's
"contract:" it returns some kind of a Thing.
> 2) If you don't override a public method, that method can be called on your subclass. If that method returns an instance of your super class, the return value will be an instance of your subclass.
Wrong, or "not necessarily so." The method returns whatever it
returns. There may be code in the method to snoop out the class of
the `this' on which it was called, and to tailor the nature of the
returned object accordingly. But unless it does something of that
kind, there's no magic that somehow replaces the object returned
with a different object of one of its subclasses.
clone() usually (*usually*) does such tailoring, but that is not
a requirement of the language. If (*if*) clone() returns the object
it obtains from super.clone() and if that is in turn obtained from
super.super.clone() and so on all the way up to Object, then the
object will have the same class as `this'. However, a particular
class is allowed to implement its clone() in some other way, and to
return an object of an "atypical" type.
> 3) You cannot override a method parameter with a different class. If you require the super method of your class to only be called from your class with a parameter that is a subclass of what the super class expects, you just check instanceof inside the method.
That'd be one way. Another would be to write two methods, one
an override and the other an augmentation:
class Super {
void method(Thing thing) { ... }
}
class Sub extends Super {
@Override
void method(Thing thing) {
if (thing instanceof MyThing) // might also allow null
method((MyThing) thing);
else
throw new WrongTypeException();
}
void method(MyThing thing) {
... // might or might not call super.method(thing)
... // and maybe do other things in addition
}
}
This way, a call whose compile-time type is MyThing (or a subclass
thereof) goes directly to the augmenting method without the overhead
of checking, while calls with other compile-time types get screened
for the proper run-time type.
--
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
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