SlideShare a Scribd company logo
1 of 23
Applying Generics Writing Type-Safe Code
Generic Class or Interface ,[object Object],[object Object],[object Object],class X<T1,T2> { private T1 ref1; private T2 ref2; public X(T1 ref1,T2 ref2) { this.ref1 = ref1; this.ref2 = ref2; } public T1 getRef1() { return ref1; } public T2 getRef2() { return ref2; } }
Primary purpose of Java generics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Prime Benefit ,[object Object],[object Object],[object Object],What’s the meaning of Type-safety?
SubTyping & WildCards ,[object Object],[object Object],[object Object]
Subtyping and the Substitution Principle   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Substitution Principle  contd. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Substitution Principle  contd. ,[object Object],[object Object],[object Object]
Bounded Type Parameter ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],public static void main(String[] args) { Integer ints[] = {7,8,5}; Double d[] = {5.3,7.44,6.008}; Float f[] = {4.3f,1.2f,2.3f}; GenTest<Integer> ref1 = new GenTest<Integer>(ints); GenTest<Double> ref2 = new GenTest<Double>(d); GenTest<Float> ref3 = new GenTest<Float>(f); ref1.sum(); ref2.sum(); ref3.sum(); }
WildCard (?) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Using ? extends T ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using ? extends T  contd. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using ? extends T  contd. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Always Remember   if a structure contains elements with a type of the form ? extends E, we can get elements out of the structure, but we cannot put elements into the structure.
? super T ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
? super T  contd. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Get & Put Principle ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Are the following code snippets correct? ,[object Object],[object Object],[object Object],[object Object],[object Object],ArrayList<Number> i; i = new ArrayList<Number>(); i.add(6.7); i.add(89); ArrayList<? super Integer> al = i;  Test your understanding Is ArrayList<String> and ArrayList<? extends String> same?
Comparison of Arrays & Lists ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Comparison & Bounds ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Maximum of a Collection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Comparison ,[object Object]
sts ,[object Object]
 

More Related Content

What's hot

Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1Syed Farjad Zia Zaidi
 
Python-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityPython-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityMohd Sajjad
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in pythonJothi Thilaga P
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming LanguageMansiSuthar3
 
Introduction To Programming with Python-4
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4Syed Farjad Zia Zaidi
 
Expert system with python -2
Expert system with python  -2Expert system with python  -2
Expert system with python -2Ahmad Hussein
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)Ali ٍSattar
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data typesMohd Sajjad
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1tanmaymodi4
 

What's hot (17)

Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityPython-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutability
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Introduction To Programming with Python-4
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4
 
Expert system with python -2
Expert system with python  -2Expert system with python  -2
Expert system with python -2
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Python strings
Python stringsPython strings
Python strings
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 

Similar to Applying Generics

Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Peter Antman
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2ppJ. C.
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
javasebeyondbasics
javasebeyondbasicsjavasebeyondbasics
javasebeyondbasicswebuploader
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Twoamiable_indian
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminarGautam Roy
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxKavitha713564
 
C intro
C introC intro
C introKamran
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Andrew Petryk
 

Similar to Applying Generics (20)

Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
javasebeyondbasics
javasebeyondbasicsjavasebeyondbasics
javasebeyondbasics
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Chap09
Chap09Chap09
Chap09
 
Savitch Ch 17
Savitch Ch 17Savitch Ch 17
Savitch Ch 17
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
Python
PythonPython
Python
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
C intro
C introC intro
C intro
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Array assignment
Array assignmentArray assignment
Array assignment
 
easyPy-Basic.pdf
easyPy-Basic.pdfeasyPy-Basic.pdf
easyPy-Basic.pdf
 
Savitch ch 17
Savitch ch 17Savitch ch 17
Savitch ch 17
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 

More from Bharat17485

Channel Based Io
Channel Based IoChannel Based Io
Channel Based IoBharat17485
 
Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded ApplicationsBharat17485
 
Interfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesInterfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesBharat17485
 
Exceptions &amp; Its Handling
Exceptions &amp; Its HandlingExceptions &amp; Its Handling
Exceptions &amp; Its HandlingBharat17485
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive WrappersBharat17485
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionBharat17485
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input OutputBharat17485
 

More from Bharat17485 (12)

Channel Based Io
Channel Based IoChannel Based Io
Channel Based Io
 
Core Java
Core JavaCore Java
Core Java
 
Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded Applications
 
Interfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesInterfaces &amp; Abstract Classes
Interfaces &amp; Abstract Classes
 
Enum
EnumEnum
Enum
 
Exceptions &amp; Its Handling
Exceptions &amp; Its HandlingExceptions &amp; Its Handling
Exceptions &amp; Its Handling
 
Jstl &amp; El
Jstl &amp; ElJstl &amp; El
Jstl &amp; El
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
 
String Handling
String HandlingString Handling
String Handling
 
Swing
SwingSwing
Swing
 

Applying Generics