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)

    Where justice is denied, where poverty is enforced, where ignorance prevails, and where any one class is made to feel that society is in an organized conspiracy to oppress, rob, and degrade them, neither persons nor property will be safe.
    Frederick Douglass (c. 1817–1895)

    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)