Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14040 > unrolled thread

generics

Started byNeil Morris <neil.morris4@googlemail.com>
First post2012-04-30 00:13 +0100
Last post2012-04-29 17:48 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#14040 — generics

FromNeil Morris <neil.morris4@googlemail.com>
Date2012-04-30 00:13 +0100
Subjectgenerics
Message-ID<Ha6dnQN32-O6VgDSnZ2dnUVZ8q-dnZ2d@brightview.co.uk>
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' 
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());
  }
}


thanks in advance

Neil

[toc] | [next] | [standalone]


#14042

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-04-29 19:39 -0400
Message-ID<4f9dd125$0$293$14726298@news.sunsite.dk>
In reply to#14040
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

[toc] | [prev] | [next] | [standalone]


#14044

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-04-29 16:48 -0700
Message-ID<Erknr.175418$s82.33273@newsfe10.iad>
In reply to#14042
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.

[toc] | [prev] | [next] | [standalone]


#14045

Frommarkspace <-@.>
Date2012-04-29 17:48 -0700
Message-ID<jnknh7$1ai$1@dont-email.me>
In reply to#14040
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web