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)

    I am both a public and a private school boy myself, having always changed schools just as the class in English in the new school was taking up Silas Marner, with the result that it was the only book in the English language that I knew until I was eighteen—but, boy, did I know Silas Marner!
    Robert Benchley (1889–1945)

    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)