Re: generics

Discussion in 'Java' started by Arne Vajhøj, Apr 30, 2012.

  1. Arne Vajhøj

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Apr 30, 2012
    #1
    1. Advertising

  2. Arne Vajhøj

    Daniel Pitts Guest

    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.
    Daniel Pitts, Apr 30, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willem Oosthuizen
    Replies:
    1
    Views:
    2,780
    Jonathan Bromley
    Jul 9, 2003
  2. Acciduzzu

    Integers only as generics?

    Acciduzzu, Sep 22, 2003, in forum: VHDL
    Replies:
    4
    Views:
    714
    Allan Herriman
    Sep 23, 2003
  3. valentin tihomirov

    generics in TB

    valentin tihomirov, Dec 17, 2003, in forum: VHDL
    Replies:
    4
    Views:
    582
    valentin tihomirov
    Dec 18, 2003
  4. Juergen Berchtel
    Replies:
    1
    Views:
    5,987
    John C. Bollinger
    May 20, 2005
  5. Soul
    Replies:
    0
    Views:
    514
Loading...

Share This Page