Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: generics

Reply
Thread Tools

Re: generics

 
 
markspace
Guest
Posts: n/a
 
      04-30-2012
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
generics in TB valentin tihomirov VHDL 4 12-18-2003 07:04 PM
Integers only as generics? Acciduzzu VHDL 4 09-23-2003 12:45 AM
Re: Multi-dimentional arrays in components using generics Willem Oosthuizen VHDL 1 07-09-2003 12:13 PM



Advertisments