• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Shay Levy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Stanley,

The explanation I provided was wrong. read Henry's explanation, It is the correct one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic