File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
1>I knwo that using generics you can create a class template using parameterized type. Just one general question though.
class Sport<T>
{
//stuff
}
is this similar to

class Sport<Object>
{
//stuff
}

???

My understanding of generics says it is safe. However i am looking for support here. And if i am wrong, could any one please explain to me the difference.

2> I would also like to know when does it make sense to create such general template classses.. Is dis a good idea technically or is dere a catch??
Shay Levy
Greenhorn

Joined: Oct 01, 2009
Posts: 7
Hi Stanley,

I am pretty new to Generics but from what I understand the two implementations you described of Sport class are not the same. Parameterized types are not polymorphic which means that the Sport<Object> class can only be instantiate with a type Object and not with any other type (even though they are Object too). On the other hand the Sport<T> class can be instantiated with any type (String,Integer,Double or types that you created).

As for the second question. A popular example is the generic Stack implementation. If you implement a Stack using generics (class MyStack<E>) than your stack could hold many types. you can instantiate one stack that will hold String elements and another that will hold Integer elements and so on. The most important thing is that you dont have to worry about type-safety issues (assuming you dont try to interact with legacy code, but that is for a diffrent topic).

I am sure some of the Bartenders and Ranch handers can give a more detailed explanation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Stanley Walker wrote:1>I knwo that using generics you can create a class template using parameterized type. Just one general question though.
class Sport<T>
{
//stuff
}
is this similar to

class Sport<Object>
{
//stuff
}

???


In the first case, your class has a generic type T. In the second case, your class has a generic type Object. Keep in mind, that in the second case, Object is *not* the java.lang.Object class. You just happened to name your generic type with the same name as the Object class -- and in effect, may cause some confusion in your class implementation.

If you want to only allow your class to be instantiated with the object type, consider using bounded types.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shay Levy
Greenhorn

Joined: Oct 01, 2009
Posts: 7
Thanks Henry. being new to generics is really confusing at the beginning. what i understood from your answer is that T and Object are equivalant in the context of class declarations : Sport<T> is the same as Sport<Object> only the latter can create confusion when used. So the polymorphism issue is only at methods declarations?

Thanks
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
ok now i am even more confused. Thank you for the answers.
let me respond one by one.

1>Shay..

lets say i declare ArrayList<Dog> ar=new ArrayList<Dog>(). i can add ar.add(new Poodle()) where Poodle extends Dog cause Poodle is a Dog , right?So going by the same logic,let me explain my confusion.
i have
class Sport<Object>
{
ArrayList<Object> ar;
Sport(ArrayList<Object> ar)
{
this.ar=ar;
}
}
so if i instantiate Sport as Sport sp=new Sport(arrayList); where arrayList contains string elements, this is wrong??? even though i have declared arrayList as ArrayList<Object> arrayList= new ArrayList<Object>(); and added string elements to it.
If i am wrong in my above understanding please do correct me.

2>Henry...
class Sport<Object>
{
} this is wrong?? compiler would not recognize this as adding any object?please explain.
and by bounded types is this what you are suggesting

class Sport<T extends Object>
{
}

??


Please do help, cause the more i dwelve into generics the more confused i seem to get.
I apologize if the questions are outright stupid.
Shay Levy
Greenhorn

Joined: Oct 01, 2009
Posts: 7
Hey Stanley,

The explanation I provided was wrong. read Henry's explanation, It is the correct one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics