views:

183

answers:

5

I am looking for an good explanation, maybe with some examples. In my understanding, something is "generic" when it can be used for multiple purposes. But I may be wrong...

+6  A: 

The root is the same as "genus" -- it means describing all the members of a group of related things.

In programming terms, it means you're describing a whole collection of things that share a common pattern or template, with only small variations. C++ templates and Java generics are, to the programmer, more or less the same thing: a way of saying "whatever the underlying type is, I want to be able to do these things." The implementaiton methods are different, but the idea is the same.

So the idea is, itself, somewhat generic.

Charlie Martin
+1  A: 

Another name for Generics is Parameterized Classes. I believe the UML uses that term.

John Topley
A: 

A generic class gets used for a specific purpose

Richard Ev
+2  A: 

An example using contrast.

Specific: The StringCollection is a collection in .Net can only hold Strings.

Generic: The Generic.List(Of T) is a collection in .Net can hold any data type T.

rvarcher
+2  A: 

Do you want a "generic" definition, or within a specific context?

In a general sense, "generic" describes a process or structure that does not itself enforce a rigid context within which it must be called. At the same time, it must understand enough about its context to be meaningful. For example, a generic structure for adding numbers would not care whether given floating-point or integer arguments, but would have difficulty adding Q + Pi.

To some extent, this is foundational to object-oriented programming. However, many programming languages use the term "generic" in a much more restrictive sense. Within those languages, a generic class can be strongly typed at run-time to any class that implements a known "interface" (in a general sense...) that the subscriber knows how to interact with. Languages vary widely on how they implement and support this, so concrete examples may not provide much more insight unless you identify the desired language up front.

GalacticCowboy
that would be objective-c, in my case. But gread explanation. I think I've got it now.
Thanks