SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Destacado

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
 

Destacado (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 a 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 a 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?
 

Último

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 

Último (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 

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