SlideShare a Scribd company logo
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 Bed
Bradford Bazemore
 
Supercomputing at Georgia Southern
Supercomputing at Georgia SouthernSupercomputing at Georgia Southern
Supercomputing at Georgia Southern
Bradford Bazemore
 
EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014EVENTAFFAIR_BROCHURE_APRILE_2014
EVENTAFFAIR_BROCHURE_APRILE_2014Sabine Vinci
 
Curriculum vitae
Curriculum vitaeCurriculum vitae
Curriculum vitae
Shaz Syed
 
My daily routine
My daily routineMy daily routine
My daily routine
arovira14
 
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
Esthervanrees
 

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 Software
vinod_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.pdf
ChaoYang81
 
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
Formulatedby
 
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
Stanojko Markovik
 
Learning and Modern Programming Languages
Learning and Modern Programming LanguagesLearning and Modern Programming Languages
Learning and Modern Programming Languages
Ray Toal
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
Ruth Marvin
 
Apex for humans
Apex for humansApex for humans
Apex for humans
Kevin Poorman
 
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 basics
Joseph Burchett
 
Automating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLPAutomating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLP
Justin Long
 
Matlab programming
Matlab programmingMatlab programming
Matlab programming
Md. Rayid Hasan Mojumder
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
Ruth 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/Rails
danielrsmith
 
The abstract art of software development
The abstract art of software developmentThe abstract art of software development
The abstract art of software development
Stephen 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

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 

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