Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generic class in a non generic class

Reply
Thread Tools

Generic class in a non generic class

 
 
nramnath@gmail.com
Guest
Posts: n/a
 
      06-17-2006
Hi,
I want to store a Generic class in a non generic class. Is it possible
??

public class A<T>
{
T t ;
public T getValue()
{
return t;
}
}

public class B
{
A a; // How to declare generic A here
public <T> A<T> getA()
{
return a;
}
}


I know this will not work. Is there a way I can get it working with
wildcards.
I am not 100% clear with generic - Is it possible at all??

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-17-2006
<(E-Mail Removed)> wrote:
> public class A<T>
> {
> T t ;
> public T getValue()
> {
> return t;
> }
> }
>
> public class B
> {
> A a; // How to declare generic A here
> public <T> A<T> getA()
> {
> return a;
> }
> }
>
>
> I know this will not work. Is there a way I can get it working with
> wildcards.
> I am not 100% clear with generic - Is it possible at all??


If I understand you correctly, then no it's not possible, at least
without warnings. That's because it's not type-safe. You are basically
saying that you want B to not keep track of the type parameter, and then
be able to return the object as if it had any type parameter and have
the compiler be okay with it. That's exactly the kind of problem that
generics were meant to solve. You certainly can do this:

public class B
{
A<?> a;
public A<?> getA() { return a; }
}

That's okay because you aren't pretending to know anything about a.
And, of course, you can do this:

public class B<T>
{
A<T> a;
public A<T> getA() { return a; }
}

That's okay because you really do know something about a. The only
thing that's truly impossible is to throw away knowledge of a's type,
and then go on as if you still know it by trying to return a as a
specific type.

What you're attempting is actually somewhat close to capture conversion.
If you had an a for which you don't know the type argument, you can
introduce an arbitrary type name to represent it for the purposes of a
calculation. However, you can't then return something typed with that
arbitrary name, since the name is meaningless outside of the method
which resulted in use of capture conversion.

--
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
nramnath@gmail.com
Guest
Posts: n/a
 
      07-04-2006
Hai,
Thanks for the reply. I too know about that, but just wanted to
confirm.
Thanks
Ramnath
Chris Smith wrote:
> <(E-Mail Removed)> wrote:
> > public class A<T>
> > {
> > T t ;
> > public T getValue()
> > {
> > return t;
> > }
> > }
> >
> > public class B
> > {
> > A a; // How to declare generic A here
> > public <T> A<T> getA()
> > {
> > return a;
> > }
> > }
> >
> >
> > I know this will not work. Is there a way I can get it working with
> > wildcards.
> > I am not 100% clear with generic - Is it possible at all??

>
> If I understand you correctly, then no it's not possible, at least
> without warnings. That's because it's not type-safe. You are basically
> saying that you want B to not keep track of the type parameter, and then
> be able to return the object as if it had any type parameter and have
> the compiler be okay with it. That's exactly the kind of problem that
> generics were meant to solve. You certainly can do this:
>
> public class B
> {
> A<?> a;
> public A<?> getA() { return a; }
> }
>
> That's okay because you aren't pretending to know anything about a.
> And, of course, you can do this:
>
> public class B<T>
> {
> A<T> a;
> public A<T> getA() { return a; }
> }
>
> That's okay because you really do know something about a. The only
> thing that's truly impossible is to throw away knowledge of a's type,
> and then go on as if you still know it by trying to return a as a
> specific type.
>
> What you're attempting is actually somewhat close to capture conversion.
> If you had an a for which you don't know the type argument, you can
> introduce an arbitrary type name to represent it for the purposes of a
> calculation. However, you can't then return something typed with that
> arbitrary name, since the name is meaningless outside of the method
> which resulted in use of capture conversion.
>
> --
> Chris Smith - Lead Software Developer / Technical Trainer
> MindIQ Corporation


 
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
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM



Advertisments