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


Groups > comp.lang.java.programmer > #35380

Re: Generics

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generics
Date 2016-08-17 15:22 -0400
Organization A noiseless patient Spider
Message-ID <np2dhf$tvo$1@dont-email.me> (permalink)
References <681a67de-8a78-46ab-a028-5176a48df402@googlegroups.com>

Show all headers | View raw


On 8/17/2016 2:59 PM, Eric Douglas wrote:
> Say we have 3 classes.
>
> public class TestA<E extends TestB> {
> }
>
> public abstract class TestB {
> }
>
> public class TestC extends TestB {
> }
>
> Now we create an instance.
>
> TestA<TestC> testMe = new TestA();

     TestA<TestC> testMe = new TestA<TestC>();

or, more briefly,

     TestA<TestC> testMe = new TestA<>();

> Now within class TestA how do we tell it instance testMe is referencing TestC?
> The only thing I could find is to add a method or constructor which passes in a reference to TestC.class or the class name, so there's that for last resort, but I do want to make sure if I have to pass it in separately that it matches the value of E.

     I'm not sure what you're asking, so I'll try answers to two
different questions -- if neither is helpful, please post again.

     1) The methods and fields and whatnot within TestA should
traffic in things of type "E", like

	public class TestA<E extends TestB> {
	    private final E theTest;

	    public TestA(E theTest) {
	        this.theTest = theTest;
	    }

	    public E getTheTest() {
	        return theTest;
	    }

	    // ...
	}

     2) If some code inside TestA needs to be aware of the TestC-ness
of some reference or other, use "instanceof":

	public class TestA<E extends TestB> {
	    private boolean isItTestC(E thing) {
	        return thing instanceof TestC;
	    }
	}

However, I'd view this need with suspicion.  The "instanceof" operator
(and its reflective counterparts) are often signs that the design
should be reconsidered -- and perhaps retained, but only after due
thought has lowered the raised eyebrows.  More commonly, it's a sign
that polymorphism is not being used to best advantage.

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Generics Eric Douglas <e.d.programmer@gmail.com> - 2016-08-17 11:59 -0700
  Re: Generics Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-08-17 21:20 +0200
  Re: Generics Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-17 15:22 -0400
    Re: Generics Eric Douglas <e.d.programmer@gmail.com> - 2016-08-17 14:03 -0700
      Re: Generics Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-17 20:06 -0400
      Re: Generics Robert Klemme <shortcutter@googlemail.com> - 2016-08-18 08:23 +0200
        Re: Generics Eric Douglas <e.d.programmer@gmail.com> - 2016-08-18 05:34 -0700

csiph-web