Object Oriented Programming: Unit IV: I/O, Generics, String Handling

Generic Methods

with Example Java Programs

Generic method allows a programmer to write a generalised method for the methods of different data types.Suppose we want to print an array of integer, float and character type elements then normally we write three different methods performing the corresponding task.

Generic Methods

Generic method allows a programmer to write a generalised method for the methods of different data types.

Suppose we want to print an array of integer, float and character type elements then normally we write three different methods performing the corresponding task. The object oriented feature of Java allows us to make use of same function name. This idea is illustrated in following Java program -

Java Program[Overload Prog1.java]

import java.io.*;

import java.util.*;

public class OverloadProg1

{

public static void display(float[] a)

{

for(int i=0;i<5;i++)

System.out.printf(" %.2f",a[i]);

}

public static void display(int[] a)

{

for(int i=0;i<5;i++)

System.out.printf("%d",a[i]);

}

public static void display(char[] a)

{

for(int i=0;i<5;i++)

System.out.printf("%c",a[i]);

}

public static void main(String[] args)

{

float[] dbl_a={11,22,33,44,55};

int[] int_a={1,2,3,4,5};

char[] char_a= {'A', 'B', 'C','D','E'};

System.out.println("\n The Float elements are ...");

display(dbl_a);

System.out.println("\n The Integer elements are ...");

display(int_a);

System.out.println("\n The character elements are ...");

display(char_a);

}

}

Output

The Float elements are...

11.00 22.00 33.00 44.00 55.00

The Integer elements are... 12345

The character elements are... ABCDE

But if we override the generic methods then the code becomes more simplistic -

Java Program[OverloadProg2.java]

import java.io.*;

import java.util.*;

public class OverloadProg2

{

public static <T> void display(T[] a) //Generic Method is created

{

for(int i=0;i<5;i++)

System.out.printf("%s",a[i]);

}

public static void main(String[] args)

{

float[] dbl a= ={11,22,33,44,55};

int[] int_a={1,2,3,4,5};

char[] char_a= {'A', 'B', 'C', 'D', 'E'};

System.out.println("\n The Float elements are ...");

display( dbl_a);

System.out.println("\n The Integer elements are ...");

display(int_a);

System.out.println("\n The character elements are ...");

display( char_a);

}

}

Output

The Float elements are...

11.00 22.00 33.00 44.00 55.00

The Integer elements are ... 12345

The character elements are ... ABCDE

Ex. 4.5.1: Write generic method for sorting an array of integer objects.

Sol. :

private <E extends Comparable<E>>void bubbleSortG(E[] Arr) {

E temp;

for(int j = 1; j < Arr.length; j++) {

for(int i = 0; i < Arr.length -j; i++) {

if(Arr[i].compareTo(Arr[i+1]) > 0) {

temp = Arr[i];

Arr[i] = Arr[i+1];

Arr[i+1] temp;

}

}

}

for(E i: Arr) {

System.out.print(" " + i);

}

}

Review Question

1. Explain about generic method with suitable example.

Object Oriented Programming: Unit IV: I/O, Generics, String Handling : Tag: : with Example Java Programs - Generic Methods