What is the difference between generics and polymorphism? I know it has something to do with compile time or binding, but I'm not sure. Please provide some code examples.
-
3Op has another similar question: http://stackoverflow.com/questions/2423231/polymorphism-relates-inheritance – Greg K Mar 11 '10 at 07:45
-
I've answered something similar at http://stackoverflow.com/questions/5854581/polymorphism-in-c/5854862#5854862 which may help. The code examples are very simple though - just enough to illustrate each concept. – Tony Delroy Jun 16 '11 at 06:00
-
2I'm voting to close this question as off-topic because if you read the question, you 'll see. Just check how it starts: "I need ans for this problem badly---"!!! – gsamaras Jul 21 '15 at 17:15
2 Answers
Polymorphism is a property of classes, in that they implement a common interface, or are derived from a base class, implementing virtual methods in a different way to reflect the different behavior of derived classes.
Generics is a property of an algorithm, or a class implementing an algorithm (sort) or a common operation (lists), requiring the classes they deal with to have certain methods, properties, or interfaces.

- 36,858
- 7
- 80
- 143
-
I like this answer more than the others, but to me "property of classes" seems to mean the same thing as "properties of an algorithm" in this context. By "class" means "state + behaviour". But if the algorithm is stateful then that is state + behaviour again. I too am trying to understand the distinction. – Sridhar Sarnobat Oct 21 '22 at 00:44
In addition to previous answers... I'll use Java, but the concept is pretty much the same. Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively.
e.g.
Using Java Generic concept, we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements.
public class GenericMethodTest
{
// generic method printArray
public static < E > void printArray( E[] inputArray )
{
// Display array elements
for ( E element : inputArray ){
System.out.printf( "%s ", element );
}
System.out.println();
}
public static void main( String args[] )
{
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println( "Array integerArray contains:" );
printArray( intArray ); // pass an Integer array
System.out.println( "\nArray doubleArray contains:" );
printArray( doubleArray ); // pass a Double array
System.out.println( "\nArray characterArray contains:" );
printArray( charArray ); // pass a Character array
}
}
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.
e.g.
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now following declarations are legal:
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;

- 6,880
- 3
- 29
- 47