Velocity Reviews

Velocity Reviews (
-   Java (
-   -   Re: generics (

markspace 04-30-2012 12:48 AM

Re: generics
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.

All times are GMT. The time now is 05:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.