SlideShare a Scribd company logo
1 of 14
1
Generics <? super Java>
By Brad Bazemore
2
Create...

a box interface
− You can put into the box
− You can take out of box
− NOTE: needs to be able to handle ALL
possible types (not just primitives!)
GO!
3
Generic Programing

generic programming is a style of
computer programming

written in terms of to-be-specified-later

instantiated when needed for specific
types provided as parameters
4
Ex1

Given N data structures

Given M algorithms

If we want an algo for every data struct we
wind up with NM methods
− NOTE: this is BAD

Using generics we end up with only N+M
methods
− NODE: this is GOOD
5
Java Generics
6
What are they for?

"a type or method to operate on objects of
various types while providing compile-time type
safety." (Java Programming Language)

Basically...
− IT MAKES LIFE BETTER!!!
7
QUIZ!

If my program has 3 types of data
structures and 5 types of algorithms...
1)How many methods will there be with NO
generics?
2)How many methods will there be WITH
generics?
GO!
8
The Syntax

ObjectName <Type Variable>

Ex2
− ArrayList<String> L=new ArrayList<String>();

Attention Test
− Create a method signature for the put
operation for a box, and it takes a
generic of some kind
GO!
9
Lo Pi Hi

Lets take it up a notch!

Ok so there is a catch, if I say <Number>
− Integer, Float, ect.

It will not work!!!

Why you ask?

Attention Test
QUIZ!
10
Quiz

How do you implement a method with
generic type that accepts all subtypes of
Number?
11
Now that I have you attention

The fancy word for this is “variance”

But so you can see the big picture

Covariant: converting from a specialized
type (Cats) to a more general type
(Animals): Every cat is an animal.

Contravariant: converting from a general
type (Shapes) to a more specialized type
(Rectangles): Is this shape a rectangle?

Invariant: not able to convert.
12
So how to make this work...

Using the Java wild card “?”
− Yes it is the

The basic idea is this
− < ? extends T >
− < ? super T >

Extends means all subtypes of T

Super means all super types of T
13
Ex3
14
See?

Any Questions?

NOTE: there is a lab

More Related Content

Viewers also liked

WBR_B2B May 24 Webcast Combined FINAL
WBR_B2B May 24 Webcast Combined FINALWBR_B2B May 24 Webcast Combined FINAL
WBR_B2B May 24 Webcast Combined FINALChris McCann
 
Raspberry Pi Cluster Test Bed
Raspberry Pi Cluster Test BedRaspberry Pi Cluster Test Bed
Raspberry Pi Cluster Test BedBradford Bazemore
 
Supercomputing at Georgia Southern
Supercomputing at Georgia SouthernSupercomputing at Georgia Southern
Supercomputing at Georgia SouthernBradford Bazemore
 
EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014Sabine Vinci
 
Curriculum vitae
Curriculum vitaeCurriculum vitae
Curriculum vitaeShaz Syed
 
My daily routine
My daily routineMy daily routine
My daily routinearovira14
 
De aanpak voor succesvolle A/B-testen
De aanpak voor succesvolle A/B-testenDe aanpak voor succesvolle A/B-testen
De aanpak voor succesvolle A/B-testenEsthervanrees
 

Viewers also liked (8)

GenderX
GenderXGenderX
GenderX
 
WBR_B2B May 24 Webcast Combined FINAL
WBR_B2B May 24 Webcast Combined FINALWBR_B2B May 24 Webcast Combined FINAL
WBR_B2B May 24 Webcast Combined FINAL
 
Raspberry Pi Cluster Test Bed
Raspberry Pi Cluster Test BedRaspberry Pi Cluster Test Bed
Raspberry Pi Cluster Test Bed
 
Supercomputing at Georgia Southern
Supercomputing at Georgia SouthernSupercomputing at Georgia Southern
Supercomputing at Georgia Southern
 
EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014
 
Curriculum vitae
Curriculum vitaeCurriculum vitae
Curriculum vitae
 
My daily routine
My daily routineMy daily routine
My daily routine
 
De aanpak voor succesvolle A/B-testen
De aanpak voor succesvolle A/B-testenDe aanpak voor succesvolle A/B-testen
De aanpak voor succesvolle A/B-testen
 

Similar to Generics lecture

Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review GuideBenjamin Kissinger
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Duckademy IT courses
 
A Grand Unified Theory of Software
A Grand Unified Theory of SoftwareA Grand Unified Theory of Software
A Grand Unified Theory of Softwarevinod_dinakaran
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java MayaTofik
 
05-transformers.pdf
05-transformers.pdf05-transformers.pdf
05-transformers.pdfChaoYang81
 
Data Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdData Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdFormulatedby
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidStanojko Markovik
 
Learning and Modern Programming Languages
Learning and Modern Programming LanguagesLearning and Modern Programming Languages
Learning and Modern Programming LanguagesRay Toal
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Top 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About RubyTop 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About RubyJeff Cohen
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basicsJoseph Burchett
 
Automating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLPAutomating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLPJustin Long
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsRuth Marvin
 
ORUG - Sept 2014 - Lesson When Learning Ruby/Rails
ORUG - Sept 2014 - Lesson When Learning Ruby/RailsORUG - Sept 2014 - Lesson When Learning Ruby/Rails
ORUG - Sept 2014 - Lesson When Learning Ruby/Railsdanielrsmith
 
The abstract art of software development
The abstract art of software developmentThe abstract art of software development
The abstract art of software developmentStephen Erdman
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?Hernan Wilkinson
 

Similar to Generics lecture (20)

Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...
 
A Grand Unified Theory of Software
A Grand Unified Theory of SoftwareA Grand Unified Theory of Software
A Grand Unified Theory of Software
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
05-transformers.pdf
05-transformers.pdf05-transformers.pdf
05-transformers.pdf
 
Data Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdData Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ Scribd
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Learning and Modern Programming Languages
Learning and Modern Programming LanguagesLearning and Modern Programming Languages
Learning and Modern Programming Languages
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Apex for humans
Apex for humansApex for humans
Apex for humans
 
Top 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About RubyTop 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About Ruby
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basics
 
Automating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLPAutomating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLP
 
Matlab programming
Matlab programmingMatlab programming
Matlab programming
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
ORUG - Sept 2014 - Lesson When Learning Ruby/Rails
ORUG - Sept 2014 - Lesson When Learning Ruby/RailsORUG - Sept 2014 - Lesson When Learning Ruby/Rails
ORUG - Sept 2014 - Lesson When Learning Ruby/Rails
 
The abstract art of software development
The abstract art of software developmentThe abstract art of software development
The abstract art of software development
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?
 

Recently uploaded

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Recently uploaded (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Generics lecture

  • 1. 1 Generics <? super Java> By Brad Bazemore
  • 2. 2 Create...  a box interface − You can put into the box − You can take out of box − NOTE: needs to be able to handle ALL possible types (not just primitives!) GO!
  • 3. 3 Generic Programing  generic programming is a style of computer programming  written in terms of to-be-specified-later  instantiated when needed for specific types provided as parameters
  • 4. 4 Ex1  Given N data structures  Given M algorithms  If we want an algo for every data struct we wind up with NM methods − NOTE: this is BAD  Using generics we end up with only N+M methods − NODE: this is GOOD
  • 6. 6 What are they for?  "a type or method to operate on objects of various types while providing compile-time type safety." (Java Programming Language)  Basically... − IT MAKES LIFE BETTER!!!
  • 7. 7 QUIZ!  If my program has 3 types of data structures and 5 types of algorithms... 1)How many methods will there be with NO generics? 2)How many methods will there be WITH generics? GO!
  • 8. 8 The Syntax  ObjectName <Type Variable>  Ex2 − ArrayList<String> L=new ArrayList<String>();  Attention Test − Create a method signature for the put operation for a box, and it takes a generic of some kind GO!
  • 9. 9 Lo Pi Hi  Lets take it up a notch!  Ok so there is a catch, if I say <Number> − Integer, Float, ect.  It will not work!!!  Why you ask?  Attention Test QUIZ!
  • 10. 10 Quiz  How do you implement a method with generic type that accepts all subtypes of Number?
  • 11. 11 Now that I have you attention  The fancy word for this is “variance”  But so you can see the big picture  Covariant: converting from a specialized type (Cats) to a more general type (Animals): Every cat is an animal.  Contravariant: converting from a general type (Shapes) to a more specialized type (Rectangles): Is this shape a rectangle?  Invariant: not able to convert.
  • 12. 12 So how to make this work...  Using the Java wild card “?” − Yes it is the  The basic idea is this − < ? extends T > − < ? super T >  Extends means all subtypes of T  Super means all super types of T