SlideShare a Scribd company logo
1 of 140
Download to read offline
January 21, 2009                                               Generic Classes in Java




                          Department of Computer Science




                                            Java Generics
                                                E. Steegmans
                                                 K.U.Leuven




                         Overview

                                      concepts
                          Basic


                                     erasure
                          Type


                              Wild cards
                          



                              Inheritance
                          



                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                         1
January 21, 2009                                                                        Generic Classes in Java




                         Non-Generic Classes
                              No formal arguments involved in definitions of
                          

                              classes and interfaces
                                   Object is often used as the most general type for
                               


                                   variables, method arguments and return types
                                    -  Typical example: Container classes in Java API
                                   Common practice before Java 1.5
                               




                         Non-Generic Classes: Usage

                          List employees = new ArrayList();

                          Person albert = …;
                          Parrot filip = …;
                          employees.add(albert);
                          employees.add(filip);
                                   // No compile-time checks nor runtime checks.


                          Person laurent = (Person) employees.get(1);
                                   // Virtual machine throws ClassCastException.




Eric Steegmans - K.U.Leuven                                                                                  2
January 21, 2009                                                                   Generic Classes in Java




                         Generics
                              Parameterized definitions of classes and
                          

                              interfaces
                                   Only reference types as formal arguments
                               


                                    -  No other types such as integers, …
                                   Convention: single letter arguments such as E
                               

                                   (element), T (type), K (key), …




                         Generic Classes
                          public class ArrayList
                              extends AbstractList
                              implements List, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(Object elem);
                            public Object get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator iterator();
                            public List subList(int from, int to);
                            public boolean addAll(Collection c);
                            public boolean containsAll(Collection c);
                            public Class getClass();
                            public Object[] toArray(Object[] a);
                            …
                          }




Eric Steegmans - K.U.Leuven                                                                             3
January 21, 2009                                                     Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>


                          {
                              public Arraylist();
                              public boolean add( E     elem);
                              public   E    get(int index);




                              …
                          }




                         Generic Classes
                          public class ArrayList<E>


                          {
                              public   Arraylist();
                              public   boolean add(E elem);
                              public   E get(int index);
                              public   boolean remove(Object o);
                              public   boolean contains(Object o);
                              public   Object[] toArray();




                              …
                          }




Eric Steegmans - K.U.Leuven                                                               4
January 21, 2009                                                                Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);




                              …
                          }




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);
                            public boolean addAll(Collection<? extends E> c);
                            public boolean containsAll(Collection<?> c);
                            public Class<? extends Object> getClass();

                              …
                          }




Eric Steegmans - K.U.Leuven                                                                          5
January 21, 2009                                                                Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);
                            public boolean addAll(Collection<? extends E> c);
                            public boolean containsAll(Collection<?> c);
                            public Class<? extends Object> getClass();
                            public <T> T[] toArray(T[] a);
                            …
                          }




                         Generics: Usage

                          List<Person> employees =
                              new ArrayList<Person>();

                          Person albert = …;
                          Parrot filip = …;
                          employees.add(albert);
                          employees.add(filip);
                              // Compile-time checks.


                          Person laurent = employees.get(1);
                              // No type cast needed.




Eric Steegmans - K.U.Leuven                                                                          6
January 21, 2009                                           Generic Classes in Java




                         Generics: Advantages
                              Correctness 
                          

                                   Compile-time checks 
                               



                              Complexity 
                          

                                   Type casts 
                               



                              Readability 
                          

                                   Static information 
                               




                         Overview

                                          concepts
                          Basic



                                          erasure
                          Type


                                        cards
                          Wild


                              Inheritance
                          



                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                     7
January 21, 2009                                                                                            Generic Classes in Java




                         Type Erasure
                              The Java compiler removes all information related
                          

                              to generic types
                                   The Java Virtual Machine only knows of “raw types”
                               


                                    -  All instances of all instantiations of a generic class (interface)
                                       belong to the same class (interface)
                              Advantage: binary compatibility
                          

                                   The introduction of generics does not require any
                               

                                   changes to the Java Virtual Machine
                                    -  All Java applications can still run on the same virtual machine




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];
                              // At the time an array is created, the
                              // type of its elements must be supplied.




                          }




Eric Steegmans - K.U.Leuven                                                                                                      8
January 21, 2009                                                             Generic Classes in Java




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                  if (elem instanceof T);
                                  // No runtime type information (RTTI)
                                  // available concerning generic args.



                              }

                          }




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                  if (elem instanceof T);

                                  new T();
                                  // No information concerning constructor
                                  // of generic argument.

                              }

                          }




Eric Steegmans - K.U.Leuven                                                                       9
January 21, 2009                                                                                  Generic Classes in Java




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                   if (elem instanceof T);

                                   new T();

                                   T theElement = (T) elem;
                                   // Unchecked cast from Object to T.
                              }

                          }




                         Type Erasure: Conclusion
                              Java 1.4: Run-time type safety
                          

                                   Java programs may include attempts to assign values of
                               

                                   improper type
                                    -  Most attempts are already caught at compile-time
                                   During the execution of a program, a variable can never
                               

                                   store a value of the wrong type
                                    -  The virtual machine checks the correctness of type casts




Eric Steegmans - K.U.Leuven                                                                                           10
January 21, 2009                                                                                               Generic Classes in Java




                         Type Erasure: Conclusion
                              Java 1.5: No runtime type safety for generic types
                          

                                   Without proper care
                               


                                    -  Instantiations of generic classes may store elements of the
                                       wrong type
                                        -  It is not impossible for a list of persons to store other objects such
                                           as bank accounts, books and trees
                                    -  Instantiations of generic classes may return objects of a type
                                       that differs from the return type
                                   With proper care
                               


                                    -  A generic class can make it impossible for clients to store
                                       elements of the wrong type
                                    -  Methods of a generic class always return objects of the type
                                       they promise




                         Overview

                              Basic concepts
                          



                                          erasure
                          Type



                                          cards
                          Wild


                          Inheritance


                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                                                                        11
January 21, 2009                                                                                  Generic Classes in Java




                         Polymorphism in Java 1.4
                              If S is a subtype of T, an object of type S can be
                          

                              assigned to a variable of type T
                                   Objects of type Woman and objects of type Man can be
                               


                                   assigned to variables of type Person
                              Can we assign an array of type S[] to a variable
                          
                              of type T[]




                         Polymorphism in Java 1.5
                              Can we assign an object of type G<S> to a
                          

                              variable of type G<T>?
                                   Assuming G is a generic class or interface, and S is a
                               


                                   true subtype of T
                                    -  Can we assign an object of type List<Woman> to a variable of
                                       type List<Person>?




Eric Steegmans - K.U.Leuven                                                                                           12
January 21, 2009                                                                                        Generic Classes in Java




                         Unbounded Wild Cards
                              A variable of type G<?> is a supertype for any
                          

                              instantiation of the generic class or interface G
                                   A variable of type List<?> can be assigned objects of
                               


                                   type List<Person>, List<Integer>, ...
                              The object referenced by a variable of type G<?>
                          
                              is more or less read-only
                                   Methods of G<E> with arguments of type E cannot be
                               


                                   invoked against variables of type G<?>
                                    -  Invocations of methods without arguments of type E may still
                                       cause state changes
                                   Variables of type G<?> act as if the class or interface is
                               

                                   instantiated with Object
                                    -  Invocations of methods declared to return an object of type E,
                                       return an object of type Object




                         Bounded Wild Cards
                              A variable of type G<? extends X> is a super-
                          

                              type for instantiations with type X or subtype of X
                                   A variable of type List<? extends Person> can be
                               


                                   assigned objects of type List<Man>, List<Woman>, ..
                                   This type of wild card specifies an upper bound for
                               

                                   actual types at the time of instantiation
                              The object referenced by a variable of type
                          
                              G<? extends X> is to some extent read-only
                                   Methods of G<E> with arguments of type E cannot be
                               

                                   invoked against variables of type G<? extends X>
                                   Variables of type G<? extends X> act as if the class
                               


                                   or interface is instantiated with X




Eric Steegmans - K.U.Leuven                                                                                                 13
January 21, 2009                                                                                  Generic Classes in Java




                         Bounded Wild Cards
                              A variable of type G<? super X> is a supertype
                          

                              for any instantiation with type X or supertype of X
                                   A variable of type List<? super Man> can be
                               


                                   assigned objects of type List<Man>, List<Person>
                                   This type of wild card specifies a lower bound for actual
                               

                                   types at the time of instantiation
                              The object referenced by a variable of type
                          
                              G<? super X> is to some extent write-only
                                   Methods of G<E> with actual argument of type E can
                               

                                   only be invoked with actual arguments of type X
                                   Invocations of methods declared to return an object of
                               


                                   type E, return an object of type Object




                         Raw Types
                              A variable of type G is a supertype for any
                          

                              instantiation of the generic class or interface G
                                   A variable of type List can be assigned objects of type
                               


                                   List<Account>, List<Person>
                              Be careful in approaching generic instantiations by
                          
                              means of raw types!
                                   Methods of G<E> with actual argument of type E can be
                               


                                   invoked against variables of type G
                                    -  The Java compiler only issues warnings for possible type
                                       mismatches
                                   Invocations of methods declared to return an object of
                               

                                   type E, return an object of type Object




Eric Steegmans - K.U.Leuven                                                                                           14
January 21, 2009                                                                          Generic Classes in Java




                         Generic Arguments: Bounds
                              A generic class or interface G<T extends C>
                          

                              can only be instantiated with C or a subtype of C
                                   All methods of class or interface C can be invoked
                               


                                   against variables of type T
                              Multiple bounds on generic arguments are
                          
                              possible as in G<T extends X & Y>




                         Wild Cards: Conclusion
                              Wild cards are needed in polymorphic
                          

                              assignments only because of type erasure
                                   Other languages such as C++, C# and Eiffel do not
                               


                                   need wild cards in variable declarations
                                   Approaching generic instantiations with raw types is
                               

                                   asking for (big) problems
                              Wild cards are useful in specifying restrictions on
                          
                              generic arguments




Eric Steegmans - K.U.Leuven                                                                                   15
January 21, 2009                                                                                    Generic Classes in Java




                         Overview
                              Basic concepts
                          




                              Type erasure
                          



                                         cards
                          Wild



                          Inheritance


                                                methods
                          Generic




                         Generic Subclasses
                              A generic class may extend a single class and
                          

                              may implement several interfaces
                                   Superclasses and superinterfaces may be instantiations
                               


                                   of generic classes, resp. generic interfaces
                                    -  Typically, formal arguments of the generic subclass are used to
                                       instantiate generic superclasses and superinterfaces
                              A generic interface may extend several interfaces
                          




Eric Steegmans - K.U.Leuven                                                                                             16
January 21, 2009                                                                           Generic Classes in Java




                         Polymorphism
                              If S<E> inherits from T<E>, can we assign an
                          

                              object of type S<C> to a variable of type T<C>?
                                   Can an object of type SortedSet<Integer> be
                               


                                   assigned to a variable of type Set<Integer>?




                         Frameworks
                              Generics (can) considerably improve the quality of
                          

                              frameworks
                                   Unfortunately they may also considerably increase the
                               


                                   complexity of frameworks
                                   Wild cards are not powerful enough to express all
                               

                                   restrictions




Eric Steegmans - K.U.Leuven                                                                                    17
January 21, 2009                                         Generic Classes in Java




                         The Composite Pattern




                         Overview
                              Basic concepts
                          




                              Type erasure
                          




                              Wild cards
                          



                          Inheritance



                                               methods
                          Generic




Eric Steegmans - K.U.Leuven                                                  18
January 21, 2009                                                                                  Generic Classes in Java




                         Generic Methods: Definition
                              A generic method is parameterized in one or more
                          

                              types
                                   As for generic classes, generic methods can only be
                               


                                   parameterized in reference types
                                    -  The generic parameters precede the return type of the method
                              Generic arguments can express dependencies
                          

                              among types of ordinary formal arguments
                                   Bounded wildcards in both directions are used for that
                               


                                   purpose
                              The actual types of generic methods are derived
                          
                              from their actual arguments
                                   The compiler rejects invocations if no matching type is
                               

                                   available




Eric Steegmans - K.U.Leuven                                                                                           19
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics

More Related Content

What's hot

Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionOregon FIRST Robotics
 
Xm Lquickref
Xm LquickrefXm Lquickref
Xm LquickrefLiquidHub
 
Xml Syntax Quick Reference
Xml Syntax Quick ReferenceXml Syntax Quick Reference
Xml Syntax Quick ReferenceLiquidHub
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Giorgio Orsi
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)SURBHI SAROHA
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaRafael Magana
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manualqaz8989
 

What's hot (20)

core java
core javacore java
core java
 
10slide
10slide10slide
10slide
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Cascon2011_5_rules+owl
Cascon2011_5_rules+owlCascon2011_5_rules+owl
Cascon2011_5_rules+owl
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 
Xm Lquickref
Xm LquickrefXm Lquickref
Xm Lquickref
 
Xml Syntax Quick Reference
Xml Syntax Quick ReferenceXml Syntax Quick Reference
Xml Syntax Quick Reference
 
11slide
11slide11slide
11slide
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012
 
Java beans
Java beansJava beans
Java beans
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 

Similar to BeJug.Org Java Generics

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Java Serialization
Java SerializationJava Serialization
Java Serializationjeslie
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsAnton Keks
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
Please find the an Example of abstarst Class.A class that is decla.pdf
Please find the an Example of abstarst Class.A class that is decla.pdfPlease find the an Example of abstarst Class.A class that is decla.pdf
Please find the an Example of abstarst Class.A class that is decla.pdfanithacells
 
Java class 3
Java class 3Java class 3
Java class 3Edureka!
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 

Similar to BeJug.Org Java Generics (9)

Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, Assertions
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Please find the an Example of abstarst Class.A class that is decla.pdf
Please find the an Example of abstarst Class.A class that is decla.pdfPlease find the an Example of abstarst Class.A class that is decla.pdf
Please find the an Example of abstarst Class.A class that is decla.pdf
 
Java class 3
Java class 3Java class 3
Java class 3
 
Java Generics
Java GenericsJava Generics
Java Generics
 

More from Stephan Janssen

Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Stephan Janssen
 
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesThe new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesStephan Janssen
 
The new Voxxed websites with JHipster, Angular and GitLab
The new Voxxed websites  with JHipster, Angular and GitLabThe new Voxxed websites  with JHipster, Angular and GitLab
The new Voxxed websites with JHipster, Angular and GitLabStephan Janssen
 
Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Stephan Janssen
 
Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Stephan Janssen
 

More from Stephan Janssen (17)

Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)
 
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesThe new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
 
The new Voxxed websites with JHipster, Angular and GitLab
The new Voxxed websites  with JHipster, Angular and GitLabThe new Voxxed websites  with JHipster, Angular and GitLab
The new Voxxed websites with JHipster, Angular and GitLab
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
 
Programming 4 kids
Programming 4 kidsProgramming 4 kids
Programming 4 kids
 
Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5
 
Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
BeJUG JAX-RS Event
BeJUG JAX-RS EventBeJUG JAX-RS Event
BeJUG JAX-RS Event
 
Whats New In Java Ee 6
Whats New In Java Ee 6Whats New In Java Ee 6
Whats New In Java Ee 6
 
Glass Fishv3 March2010
Glass Fishv3 March2010Glass Fishv3 March2010
Glass Fishv3 March2010
 
Advanced Scrum
Advanced ScrumAdvanced Scrum
Advanced Scrum
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Intro To OSGi
Intro To OSGiIntro To OSGi
Intro To OSGi
 
Kick Start Jpa
Kick Start JpaKick Start Jpa
Kick Start Jpa
 
BeJUG - Di With Spring
BeJUG - Di With SpringBeJUG - Di With Spring
BeJUG - Di With Spring
 
BeJUG - Spring 3 talk
BeJUG - Spring 3 talkBeJUG - Spring 3 talk
BeJUG - Spring 3 talk
 

Recently uploaded

Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceOpsTree solutions
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptx
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptxCOMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptx
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptxabalosyvonne42
 
ict grade 12 lesson 2 sinhala medium notes pdf
ict grade 12 lesson 2 sinhala medium notes pdfict grade 12 lesson 2 sinhala medium notes pdf
ict grade 12 lesson 2 sinhala medium notes pdfruhisiya9
 
LLM Threats: Prompt Injections and Jailbreak Attacks
LLM Threats: Prompt Injections and Jailbreak AttacksLLM Threats: Prompt Injections and Jailbreak Attacks
LLM Threats: Prompt Injections and Jailbreak AttacksThien Q. Tran
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.Unleashing the power of AI in UiPath Studio with UiPath Autopilot.
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.DianaGray10
 
Retrofitting for the Built Environment - IES
Retrofitting for the Built Environment - IESRetrofitting for the Built Environment - IES
Retrofitting for the Built Environment - IESIES VE
 
Grade 10 Computer Lesson 5: What is Blogging?
Grade 10 Computer Lesson 5: What is Blogging?Grade 10 Computer Lesson 5: What is Blogging?
Grade 10 Computer Lesson 5: What is Blogging?rosariokimberlyannma
 
What Developers Need to Unlearn for High Performance NoSQL
What Developers Need to Unlearn for High Performance NoSQLWhat Developers Need to Unlearn for High Performance NoSQL
What Developers Need to Unlearn for High Performance NoSQLScyllaDB
 
RTL Design Methodologies_Object Automation Inc
RTL Design Methodologies_Object Automation IncRTL Design Methodologies_Object Automation Inc
RTL Design Methodologies_Object Automation IncObject Automation
 
Future Research Directions for Augmented Reality
Future Research Directions for Augmented RealityFuture Research Directions for Augmented Reality
Future Research Directions for Augmented RealityMark Billinghurst
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum ComputingGDSC PJATK
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
AI-based audio transcription solutions (IDP)
AI-based audio transcription solutions (IDP)AI-based audio transcription solutions (IDP)
AI-based audio transcription solutions (IDP)KapilVaidya4
 
Tracking license compliance made easy - intro to Grant (OSS)
Tracking license compliance made easy - intro to Grant (OSS)Tracking license compliance made easy - intro to Grant (OSS)
Tracking license compliance made easy - intro to Grant (OSS)Anchore
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceMartin Humpolec
 

Recently uploaded (20)

Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer Experience
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptx
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptxCOMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptx
COMPUTER_GROUP 7_10 ST. JOHN VIANNEY.pptx
 
ict grade 12 lesson 2 sinhala medium notes pdf
ict grade 12 lesson 2 sinhala medium notes pdfict grade 12 lesson 2 sinhala medium notes pdf
ict grade 12 lesson 2 sinhala medium notes pdf
 
LLM Threats: Prompt Injections and Jailbreak Attacks
LLM Threats: Prompt Injections and Jailbreak AttacksLLM Threats: Prompt Injections and Jailbreak Attacks
LLM Threats: Prompt Injections and Jailbreak Attacks
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.Unleashing the power of AI in UiPath Studio with UiPath Autopilot.
Unleashing the power of AI in UiPath Studio with UiPath Autopilot.
 
Retrofitting for the Built Environment - IES
Retrofitting for the Built Environment - IESRetrofitting for the Built Environment - IES
Retrofitting for the Built Environment - IES
 
Grade 10 Computer Lesson 5: What is Blogging?
Grade 10 Computer Lesson 5: What is Blogging?Grade 10 Computer Lesson 5: What is Blogging?
Grade 10 Computer Lesson 5: What is Blogging?
 
What Developers Need to Unlearn for High Performance NoSQL
What Developers Need to Unlearn for High Performance NoSQLWhat Developers Need to Unlearn for High Performance NoSQL
What Developers Need to Unlearn for High Performance NoSQL
 
RTL Design Methodologies_Object Automation Inc
RTL Design Methodologies_Object Automation IncRTL Design Methodologies_Object Automation Inc
RTL Design Methodologies_Object Automation Inc
 
Future Research Directions for Augmented Reality
Future Research Directions for Augmented RealityFuture Research Directions for Augmented Reality
Future Research Directions for Augmented Reality
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
AI-based audio transcription solutions (IDP)
AI-based audio transcription solutions (IDP)AI-based audio transcription solutions (IDP)
AI-based audio transcription solutions (IDP)
 
Tracking license compliance made easy - intro to Grant (OSS)
Tracking license compliance made easy - intro to Grant (OSS)Tracking license compliance made easy - intro to Grant (OSS)
Tracking license compliance made easy - intro to Grant (OSS)
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your Salesforce
 

BeJug.Org Java Generics

  • 1. January 21, 2009 Generic Classes in Java Department of Computer Science Java Generics E. Steegmans K.U.Leuven Overview concepts  Basic erasure  Type Wild cards   Inheritance   Generic methods   Eric Steegmans - K.U.Leuven 1
  • 2. January 21, 2009 Generic Classes in Java Non-Generic Classes No formal arguments involved in definitions of   classes and interfaces Object is often used as the most general type for   variables, method arguments and return types -  Typical example: Container classes in Java API Common practice before Java 1.5   Non-Generic Classes: Usage List employees = new ArrayList(); Person albert = …; Parrot filip = …; employees.add(albert); employees.add(filip); // No compile-time checks nor runtime checks. Person laurent = (Person) employees.get(1); // Virtual machine throws ClassCastException. Eric Steegmans - K.U.Leuven 2
  • 3. January 21, 2009 Generic Classes in Java Generics Parameterized definitions of classes and   interfaces Only reference types as formal arguments   -  No other types such as integers, … Convention: single letter arguments such as E   (element), T (type), K (key), … Generic Classes public class ArrayList extends AbstractList implements List, Cloneable, Serializable { public Arraylist(); public boolean add(Object elem); public Object get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator iterator(); public List subList(int from, int to); public boolean addAll(Collection c); public boolean containsAll(Collection c); public Class getClass(); public Object[] toArray(Object[] a); … } Eric Steegmans - K.U.Leuven 3
  • 4. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> { public Arraylist(); public boolean add( E elem); public E get(int index); … } Generic Classes public class ArrayList<E> { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); … } Eric Steegmans - K.U.Leuven 4
  • 5. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); … } Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); public boolean addAll(Collection<? extends E> c); public boolean containsAll(Collection<?> c); public Class<? extends Object> getClass(); … } Eric Steegmans - K.U.Leuven 5
  • 6. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); public boolean addAll(Collection<? extends E> c); public boolean containsAll(Collection<?> c); public Class<? extends Object> getClass(); public <T> T[] toArray(T[] a); … } Generics: Usage List<Person> employees = new ArrayList<Person>(); Person albert = …; Parrot filip = …; employees.add(albert); employees.add(filip); // Compile-time checks. Person laurent = employees.get(1); // No type cast needed. Eric Steegmans - K.U.Leuven 6
  • 7. January 21, 2009 Generic Classes in Java Generics: Advantages Correctness    Compile-time checks    Complexity    Type casts    Readability    Static information    Overview concepts  Basic erasure  Type cards  Wild Inheritance   Generic methods   Eric Steegmans - K.U.Leuven 7
  • 8. January 21, 2009 Generic Classes in Java Type Erasure The Java compiler removes all information related   to generic types The Java Virtual Machine only knows of “raw types”   -  All instances of all instantiations of a generic class (interface) belong to the same class (interface) Advantage: binary compatibility   The introduction of generics does not require any   changes to the Java Virtual Machine -  All Java applications can still run on the same virtual machine Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; // At the time an array is created, the // type of its elements must be supplied. } Eric Steegmans - K.U.Leuven 8
  • 9. January 21, 2009 Generic Classes in Java Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); // No runtime type information (RTTI) // available concerning generic args. } } Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); new T(); // No information concerning constructor // of generic argument. } } Eric Steegmans - K.U.Leuven 9
  • 10. January 21, 2009 Generic Classes in Java Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); new T(); T theElement = (T) elem; // Unchecked cast from Object to T. } } Type Erasure: Conclusion Java 1.4: Run-time type safety   Java programs may include attempts to assign values of   improper type -  Most attempts are already caught at compile-time During the execution of a program, a variable can never   store a value of the wrong type -  The virtual machine checks the correctness of type casts Eric Steegmans - K.U.Leuven 10
  • 11. January 21, 2009 Generic Classes in Java Type Erasure: Conclusion Java 1.5: No runtime type safety for generic types   Without proper care   -  Instantiations of generic classes may store elements of the wrong type -  It is not impossible for a list of persons to store other objects such as bank accounts, books and trees -  Instantiations of generic classes may return objects of a type that differs from the return type With proper care   -  A generic class can make it impossible for clients to store elements of the wrong type -  Methods of a generic class always return objects of the type they promise Overview Basic concepts   erasure  Type cards  Wild  Inheritance Generic methods   Eric Steegmans - K.U.Leuven 11
  • 12. January 21, 2009 Generic Classes in Java Polymorphism in Java 1.4 If S is a subtype of T, an object of type S can be   assigned to a variable of type T Objects of type Woman and objects of type Man can be   assigned to variables of type Person Can we assign an array of type S[] to a variable   of type T[] Polymorphism in Java 1.5 Can we assign an object of type G<S> to a   variable of type G<T>? Assuming G is a generic class or interface, and S is a   true subtype of T -  Can we assign an object of type List<Woman> to a variable of type List<Person>? Eric Steegmans - K.U.Leuven 12
  • 13. January 21, 2009 Generic Classes in Java Unbounded Wild Cards A variable of type G<?> is a supertype for any   instantiation of the generic class or interface G A variable of type List<?> can be assigned objects of   type List<Person>, List<Integer>, ... The object referenced by a variable of type G<?>   is more or less read-only Methods of G<E> with arguments of type E cannot be   invoked against variables of type G<?> -  Invocations of methods without arguments of type E may still cause state changes Variables of type G<?> act as if the class or interface is   instantiated with Object -  Invocations of methods declared to return an object of type E, return an object of type Object Bounded Wild Cards A variable of type G<? extends X> is a super-   type for instantiations with type X or subtype of X A variable of type List<? extends Person> can be   assigned objects of type List<Man>, List<Woman>, .. This type of wild card specifies an upper bound for   actual types at the time of instantiation The object referenced by a variable of type   G<? extends X> is to some extent read-only Methods of G<E> with arguments of type E cannot be   invoked against variables of type G<? extends X> Variables of type G<? extends X> act as if the class   or interface is instantiated with X Eric Steegmans - K.U.Leuven 13
  • 14. January 21, 2009 Generic Classes in Java Bounded Wild Cards A variable of type G<? super X> is a supertype   for any instantiation with type X or supertype of X A variable of type List<? super Man> can be   assigned objects of type List<Man>, List<Person> This type of wild card specifies a lower bound for actual   types at the time of instantiation The object referenced by a variable of type   G<? super X> is to some extent write-only Methods of G<E> with actual argument of type E can   only be invoked with actual arguments of type X Invocations of methods declared to return an object of   type E, return an object of type Object Raw Types A variable of type G is a supertype for any   instantiation of the generic class or interface G A variable of type List can be assigned objects of type   List<Account>, List<Person> Be careful in approaching generic instantiations by   means of raw types! Methods of G<E> with actual argument of type E can be   invoked against variables of type G -  The Java compiler only issues warnings for possible type mismatches Invocations of methods declared to return an object of   type E, return an object of type Object Eric Steegmans - K.U.Leuven 14
  • 15. January 21, 2009 Generic Classes in Java Generic Arguments: Bounds A generic class or interface G<T extends C>   can only be instantiated with C or a subtype of C All methods of class or interface C can be invoked   against variables of type T Multiple bounds on generic arguments are   possible as in G<T extends X & Y> Wild Cards: Conclusion Wild cards are needed in polymorphic   assignments only because of type erasure Other languages such as C++, C# and Eiffel do not   need wild cards in variable declarations Approaching generic instantiations with raw types is   asking for (big) problems Wild cards are useful in specifying restrictions on   generic arguments Eric Steegmans - K.U.Leuven 15
  • 16. January 21, 2009 Generic Classes in Java Overview Basic concepts   Type erasure   cards  Wild  Inheritance methods  Generic Generic Subclasses A generic class may extend a single class and   may implement several interfaces Superclasses and superinterfaces may be instantiations   of generic classes, resp. generic interfaces -  Typically, formal arguments of the generic subclass are used to instantiate generic superclasses and superinterfaces A generic interface may extend several interfaces   Eric Steegmans - K.U.Leuven 16
  • 17. January 21, 2009 Generic Classes in Java Polymorphism If S<E> inherits from T<E>, can we assign an   object of type S<C> to a variable of type T<C>? Can an object of type SortedSet<Integer> be   assigned to a variable of type Set<Integer>? Frameworks Generics (can) considerably improve the quality of   frameworks Unfortunately they may also considerably increase the   complexity of frameworks Wild cards are not powerful enough to express all   restrictions Eric Steegmans - K.U.Leuven 17
  • 18. January 21, 2009 Generic Classes in Java The Composite Pattern Overview Basic concepts   Type erasure   Wild cards    Inheritance methods  Generic Eric Steegmans - K.U.Leuven 18
  • 19. January 21, 2009 Generic Classes in Java Generic Methods: Definition A generic method is parameterized in one or more   types As for generic classes, generic methods can only be   parameterized in reference types -  The generic parameters precede the return type of the method Generic arguments can express dependencies   among types of ordinary formal arguments Bounded wildcards in both directions are used for that   purpose The actual types of generic methods are derived   from their actual arguments The compiler rejects invocations if no matching type is   available Eric Steegmans - K.U.Leuven 19