Generics in Java - Generic Class Definitions

Generic Class Definitions

Here is an example of a generic class:

public class Entry { private final K key; private final V value; public Entry(K k,V v) { key = k; value = v; } public K getKey { return key; } public V getValue { return value; } public String toString { return "(" + key + ", " + value + ")"; } }

Note that the above class is for illustration of Java generics only.

This generic class can be used in the following way:

Entry grade440 = new Entry("mike", "A"); Entry marks440 = new Entry("mike", 100); System.out.println("grade: " + grade440); System.out.println("marks: " + marks440);

Read more about this topic:  Generics In Java

Famous quotes containing the words generic, class and/or definitions:

    “Mother” has always been a generic term synonymous with love, devotion, and sacrifice. There’s always been something mystical and reverent about them. They’re the Walter Cronkites of the human race . . . infallible, virtuous, without flaws and conceived without original sin, with no room for ambivalence.
    Erma Bombeck (20th century)

    During the long ages of class rule, which are just beginning to cease, only one form of sovereignty has been assigned to all men—that, namely, over all women. Upon these feeble and inferior companions all men were permitted to avenge the indignities they suffered from so many men to whom they were forced to submit.
    Mary Putnam Jacobi (1842–1906)

    The loosening, for some people, of rigid role definitions for men and women has shown that dads can be great at calming babies—if they take the time and make the effort to learn how. It’s that time and effort that not only teaches the dad how to calm the babies, but also turns him into a parent, just as the time and effort the mother puts into the babies turns her into a parent.
    Pamela Patrick Novotny (20th century)