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

Generic Programming

Java Object Oriented Programming

Generic is a mechanism for creating a general model in which generic methods and generic classes enable programmers to specify a single method (or a set of related methods) and single class (or a set of related classes) for performing the desired task.

Generic Programming

Generic is a mechanism for creating a general model in which generic methods and generic classes enable programmers to specify a single method (or a set of related methods) and single class (or a set of related classes) for performing the desired task.

For example -

Suppose we want to create a stack and if we create a stack of integers then it will store only the integer elements, if we try to push any string or any double type element then a compile time error will occur. If we want to push any string then we need to create a separate stack, separate class and separate methods for handling the String elements. Same is true for the elements of any other data type. This results in complex code building. To avoid such complexity, a concept of generic programming is introduced.

What is the need for Generic?

Following features show the importance of generics in Java.

1. It saves the programmers burden of creating separate methods for handling data belonging to different data types.

2. It allows the code reusability.

3. Compact code can be created.

Review Question

1. Mention the motivations of generic programming

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