Generics and polymorphism

13,180

Solution 1

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.

Solution 2

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;
Share:
13,180
Kelly
Author by

Kelly

Updated on June 04, 2022

Comments

  • Kelly
    Kelly over 1 year

    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.

    • Greg K
      Greg K almost 14 years
      Op has another similar question: stackoverflow.com/questions/2423231/…
    • Tony Delroy
      Tony Delroy over 12 years
      I've answered something similar at stackoverflow.com/questions/5854581/polymorphism-in-c/… which may help. The code examples are very simple though - just enough to illustrate each concept.
    • gsamaras
      gsamaras over 8 years
      I'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---"!!!