Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb Newsgroups: comp.lang.java.programmer Subject: Re: How to assign an object to another in it method? Date: Thu, 13 Jul 2017 16:49:48 -0000 (UTC) Organization: A noiseless patient Spider Lines: 32 Message-ID: References: <46334961-cc18-491f-828c-32e7401c7832@googlegroups.com> Reply-To: avl@logic.at Injection-Date: Thu, 13 Jul 2017 16:49:48 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="891ec2ce37e0ba46fd2b2385f5ad4a6c"; logging-data="18361"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KWXvU2uTU/CUT1MYUAzH8" User-Agent: slrn/1.0.2 (Linux) Cancel-Lock: sha1:s3yX06eJOMgmWD3Az+jaqXC16RQ= Xref: csiph.com comp.lang.java.programmer:36900 Martin Gregorie wrote: > On Thu, 13 Jul 2017 10:08:52 +0000, Andreas Leitgeb wrote: >> Ideally, a subclass should be allowed to "remove" methods in a way that >> the compiler could preempt some mistakes: "If you already know you're >> dealing with a Square, then rather use Square's setDimension(int length) >> instead of the inherited setDimensions(int,int)". Obviously the compiler >> couldn't catch cases like ((Rectangle)square).setDimensions(1,2), so not >> all runtime errors can be prevented at compile time, but at least some. > In this case why wouldn't you override Rectangle.setDimensions() with a > variant that throws an exception if the two dimensions differ Because such an exception would only be thrown at runtime. My aim is to help the compiler find bugs ahead of time in as many as feasible cases. At this time, there just doesn't seem to be a way to prevent a call like "square.setDimensions( i1, i2 );" already at compile time. One could perhaps tag it as @Deprecated, but that'll only lead to a compile time warning, not an error. (I haven't tried it, though) Imho, it is not a violation of "is-a", if a subclass declares some base-class's method as "not applicable" to this specialization. An unmodifiable Set is still a Set. But rather than saying "Come on, try to modify me and see if I care!" it could preferably say "Hey, you *know* I'm unmodifiable! Don't you even *try*!" (and where the caller *doesn't* know, only *then* callee would fall back to throwing an exception at runtime)