15. Generics.pdf - JAVA Means DURGA SIR 74 DURGASOFT...

This preview shows page 1 - 8 out of 22 pages.

JAVA Means DURGA SIR 74 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
Image of page 1

Subscribe to view the full document.

JAVA Means DURGA SIR 75 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, GENERICS Agenda: 1. Introduction 2. Type-Safety 3. Type-Casting 4. Generic Classes 5. Bounded Types 6. Generic methods and wild card character(?) 7. Communication with non generic code 8. Conclusions Introduction: Deff : The main objective of Generics is to provide Type-Safety and to resolve Type-Casting problems. Case 1: Type-Safety Arrays are always type safe that is we can give the guarantee for the type of elements present inside array. For example if our programming requirement is to hold String type of objects it is recommended to use String array. In the case of string
Image of page 2
JAVA Means DURGA SIR 76 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, array we can add only string type of objects by mistake if we are trying to add any other type we will get compile time error. Example: That is we can always provide guarantee for the type of elements present inside array and hence arrays are safe to use with respect to type that is arrays are type safe. But collections are not type safe that is we can't provide any guarantee for the type of elements present inside collection.
Image of page 3

Subscribe to view the full document.

JAVA Means DURGA SIR 77 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, For example if our programming requirement is to hold only string type of objects it is never recommended to go for ArrayList. By mistake if we are trying to add any other type we won't get any compile time error but the program may fail at runtime. Example: Hence we can't provide guarantee for the type of elements present inside collections that is collections are not safe to use with respect to type. Case 2: Type-Casting In the case of array at the time of retrieval it is not required to perform any type casting.
Image of page 4
JAVA Means DURGA SIR 78 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, Example: But in the case of collection at the time of retrieval compulsory we should perform type casting otherwise we will get compile time error.
Image of page 5

Subscribe to view the full document.

JAVA Means DURGA SIR 79 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, Example: That is in collections type casting is bigger headache. To overcome the above problems of collections(type-safety, type casting)sun people introduced generics concept in 1.5v hence the main objectives of generics are: 1. To provide type safety to the collections. 2. To resolve type casting problems. To hold only string type of objects we can create a generic version of ArrayList as follows. Example:
Image of page 6
JAVA Means DURGA SIR 80 DURGASOFT, # 202,2 nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, For this ArrayList we can add only string type of objects by mistake if we are trying to add any other type we will get compile time error that is through generics we are getting type safety.
Image of page 7

Subscribe to view the full document.

Image of page 8
You've reached the end of this preview.
  • Fall '18
  • Sateesh
  • Type system

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern