Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: generics

 
 
Arne Vajh°j
Guest
Posts: n/a
 
      04-29-2012
On 4/29/2012 7:13 PM, Neil Morris wrote:
> With the following code, what is the difference from one written with
> Bounded Type Parameters? the code has type 'Number' with the 'add'
> method using the 'Integer' type. How can I stop a subtype from being
> passed to the 'add' method?
>
> public class Test<T> {
> private T t;
> public void add(T t) {
> this.t = t;
> }
> public T get() {
> return t;
> }
> public static void main(String[] args) {
> Test<Number> test = new Test<Number>();
> test.add(new Integer(10));
> System.out.println(test.get());
> }
> }


1) Since Number is abstract then it will always be instances
of subclasses that are passed.

2) The ability to pass subclasses or classes implementing interfaces
is an essential part of OOP - preventing that is not good.

3) If you really want to block it then get the required type
stored and make a very ugly test on type in add.

Arne

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      04-29-2012
On 4/29/12 4:39 PM, Arne Vajh°j wrote:
> On 4/29/2012 7:13 PM, Neil Morris wrote:
>> With the following code, what is the difference from one written with
>> Bounded Type Parameters? the code has type 'Number' with the 'add'
>> method using the 'Integer' type. How can I stop a subtype from being
>> passed to the 'add' method?
>>
>> public class Test<T> {
>> private T t;
>> public void add(T t) {
>> this.t = t;
>> }
>> public T get() {
>> return t;
>> }
>> public static void main(String[] args) {
>> Test<Number> test = new Test<Number>();
>> test.add(new Integer(10));
>> System.out.println(test.get());
>> }
>> }

>
> 1) Since Number is abstract then it will always be instances
> of subclasses that are passed.
>
> 2) The ability to pass subclasses or classes implementing interfaces
> is an essential part of OOP - preventing that is not good.
>
> 3) If you really want to block it then get the required type
> stored and make a very ugly test on type in add.

In the context of generics though, his question brings on new meaning.
It also sounds like a homework assignment.

When declaring the "type" of the "test" variable in main, you can say
"Test<? extends Number> test", which would mean that the "T" type in
Test "is some unknown type that extends Number". You will no longer be
able to pass *anything* into add.

The converse operation is "Test<? super Number>" which means that T can
"hold a Number", but not much else.

These are more useful when passing around collections of various sorts.

public void addStuff(Collection<? super Stuff> stuffs) {
collection.add(new Stuff());
collection.add(new SomethingThatExtendsStuff();
}

public void processStuff(Iterable<? extends Stuff> stuffs) {
for (Stuff stuff: stuffs) {
stuff.process();
}
}

Hope this helps.
 
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