• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Classes

 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been assigned a task of using "generics" for the classes in my project, and I wasn't even aware that generics can be used for classes!!
I've been reading up on it and I have discovered that it can be done, for example:


Pretty impressive. However, I am not very clear about the need for this or how one would go about implementing similar changes for a a real-time project.
My questions are:
1. Would I try to make only the abstract classes generic?
2. What about interfaces?
3. What are the usual areas where such generic classes are most useful?

Thanks guys.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Would I try to make only the abstract classes generic?

No, why do you think this should be limited to abstract classes only? There's no reason to limit it to abstract classes.

2. What about interfaces?

Yes, you can also make interfaces with generics.

3. What are the usual areas where such generic classes are most useful?

The most obvious example is the collection classes: now you can make a List of a specific type of object, for example a List of Strings by writing List<String>.
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
1
[i]3. What are the usual areas where such generic classes are most useful?


The most obvious example is the collection classes: now you can make a List of a specific type of object, for example a List of Strings by writing List<String>.



Yes ofcourse, I am aware of using generics for collections, just wasn't aware of how they can be used with classes.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by v ray:

Pretty impressive. However, I am not very clear about the need for this or how one would go about implementing similar changes for a a real-time project.



An example? At the moment I'm working on an economical calculation engine. Generics (and Mark Interfaces to be honest..) helped us a lot in reducing code, make it clearer, safer, flexible.



Originally posted by v ray:

1. Would I try to make only the abstract classes generic?


I agree with Jesper, you are not forced in any way to do that. But, at least in the project above, I found very useful implementing almost all the logic in ABSTRACT classes making a VERY VERY VERY heavy use of generics and interfaces. We created then a bunch of specific implementation where we make the generics concrete (hiding so the complexity of the structure for the "low-level-developer"), class declarations easier to read/write and so on



Originally posted by v ray:

3. What are the usual areas where such generic classes are most useful?


MY OPINION is that they are more useful in containers, rules defining/executing classes, utilities class. Especially if their use is closely bound to interfaces.

Don't hesitate to ask for more details if you want
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by v ray:
Yes ofcourse, I am aware of using generics for collections, just wasn't aware of how they can be used with classes.


Well, the collection classes such as HashMap, ArrayList, TreeSet etc. are all generic classes, exactly like your class BasicGeneric<A>.
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bianchi Francesco:

MY OPINION is that they are more useful in containers, rules defining/executing classes, utilities class. Especially if their use is closely bound to interfaces.

Don't hesitate to ask for more details if you want



I really would like more details, but I'm going to start trying to implement this and post questions, maybe its better that way?
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by v ray:


I really would like more details, but I'm going to start trying to implement this and post questions, maybe its better that way?



Indeed
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic