Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #14045
| From | markspace <-@.> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: generics |
| Date | 2012-04-29 17:48 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <jnknh7$1ai$1@dont-email.me> (permalink) |
| References | <Ha6dnQN32-O6VgDSnZ2dnUVZ8q-dnZ2d@brightview.co.uk> |
On 4/29/2012 4:13 PM, Neil Morris wrote:
> Dear newsgroup
>
> With the following code, what is the difference from one written with
> Bounded Type Parameters? the code has type 'Number' with the 'add'
I'm not sure what you are asking here. Bounded type parameters place a
bound on the type. Can you give an example for us to compare with?
> method using the 'Integer' type. How can I stop a subtype from being
> passed to the 'add' method?
As pointed out, Number is abstract. You can't usefully restrict the
programmer from using subclasses of Number.
But even if you do this:
class Test {
private Number t;
public void add( Number n ) { t = n; }
...
}
The user can STILL pass a subclass. Any normal test you do
("instanceof") will still pass off an Integer as a Number.
If you own the hierarchy, you can restrict subclassing:
final class MyNumber {...
But don't try to restrict the type if someone else owns the hierarchy.
They have already determined that a class and its subclasses ARE THE
SAME THING, and you should treat them as such.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
generics Neil Morris <neil.morris4@googlemail.com> - 2012-04-30 00:13 +0100
Re: generics Arne Vajhøj <arne@vajhoej.dk> - 2012-04-29 19:39 -0400
Re: generics Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-04-29 16:48 -0700
Re: generics markspace <-@.> - 2012-04-29 17:48 -0700
csiph-web