generic programming in java - PowerPoint PPT Presentation

About This Presentation
Title:

generic programming in java

Description:

www.acem.edu.in – PowerPoint PPT presentation

Number of Views:81
Slides: 14
Provided by: Ansh_s
Tags:

less

Transcript and Presenter's Notes

Title: generic programming in java


1
Presented ByAnshu Sharma
  • Object Oriented Using Java -
  • Generic in Java

2
Generics in Java
  • Generics in Java is similar to templates in C.
    The idea is to allow type (Integer, String, etc
    and user defined types) to be a parameter to
    methods, classes and interfaces. For example,
    classes like HashSet, ArrayList, HashMap, etc use
    generics very well. We can use them for any type.

3
Generic Class
  • we use ltgt to specify parameter types in generic
    class creation. To create objects of generic
    class, we use following syntax.
  • // To create an instance of generic class
  • BaseType ltTypegt obj new BaseType ltTypegt()
  • Note In Parameter type we can not use
    primitives like 'int','char' or 'double'.

4
Examples Generic Class
  • / A Simple Java program to show working of user
    defined Generic classes
  • // We use lt gt to specify Parameter type
  • class TestltTgt
  •     // An object of type T is declared
  •     T obj
  •     Test(T obj)
  •  
  • this.obj obj 
  •   // constructor
  •     
  • public T getObject() 
  • return this.obj
  •    

5
Example contd.
  • class Main
  •     public static void main (String args)
  •     
  •         // instance of Integer type
  •         Test ltIntegergt iObj new
    TestltIntegergt(15)
  •         System.out.println(iObj.getObject())
  •    
  •         // instance of String type
  •         Test ltStringgt sObj
  •                           new TestltStringgt("GeeksF
    orGeeks")
  •         System.out.println(sObj.getObject())
  •     

6
multiple Type parameters in Generic classes
  • We can also pass multiple Type parameters in
    Generic classes.
  • // A Simple Java program to show multiple
  • // type parameters in Java Generics
  •   
  • // We use lt gt to specify Parameter type
  • class TestltT, Ugt
  •     T obj1  // An object of type T
  •     U obj2  // An object of type U
  •   
  •     // constructor
  •     Test(T obj1, U obj2)
  •     
  •         this.obj1 obj1
  •         this.obj2 obj2
  •     
  •   

7
Example contd.
  • // To print objects of T and U
  •     public void print()
  •     
  •         System.out.println(obj1)
  •         System.out.println(obj2)
  •     
  •   
  • // Driver class to test above
  • class Main
  •     public static void main (String args)
  •     
  •         Test ltString, Integergt obj
  •             new TestltString, Integergt("GfG", 15)
  •   
  •         obj.print()
  •     

8
Generic Functions
  • We can also write generic functions that can be
    called with different types of arguments based on
    the type of arguments passed to generic method,
    the compiler handles each method.

9
Example
  • // A Simple Java program to show working of user
    defined
  • // Generic functions
  •    
  • class Test
  •     // A Generic method example
  •     static ltTgt void genericDisplay (T element)
  •     
  •         System.out.println(element.getClass().getN
    ame()
  •                            " " element)
  •     
  •    
  •   

10
Example contd.
  • // Driver method
  •    public static void main(String args)
  •     
  •          // Calling generic method with Integer
    argument
  •         genericDisplay(11)
  •    
  •         // Calling generic method with String
    argument
  •         genericDisplay("GeeksForGeeks")
  •    
  •         // Calling generic method with double
    argument
  •         genericDisplay(1.0)
  •     

11
Advantages of Java Generics
  • 1. Code Reusability
  • Generics allow us to write code that will work
    with different types of data. For example,
  • public ltTgt void genericsMethod(T data) ...
  • Here, we have created a generics method. This
    method can be used to perform operations on
    integer data, string data and so on.

12
Advantages contd.
  • 2. Compile-time Type Checking
  • The type parameter of generics provides
    information about the type of data used in the
    generics code.
  • Hence, any error can be identified at compile
    time which is easier to fix than runtime errors.
    For example,
  • // without using Generics
  • NormalClass list new NormalClass() // calls
    method of NormalClass
  • list.display("String")
  • In the above code, we have a normal class. We
    call the method named display() of this class by
    passing a string data.
  • Here, the compiler does not know if the value
    passed in the argument is correct or not.

13
Advantages contd.
  • However, let's see what will happen if we use the
    generics class instead.
  • // using Generics
  • GenericsClassltIntegergt list new
    GenericsClassltgt() // calls method of
    GenericsClass
  • list2.display("String")
  • In the above code, we have a generics class.
    Here, the type parameter indicates that the class
    is working on Integer data. Hence when the string
    data is passed in argument, the compiler will
    generate an error.
Write a Comment
User Comments (0)
About PowerShow.com