Comp 212 Lecture 18: Java Generics
In this lecture, Anupam Chanda covers Java generics, including parameterized classes and methods, type safety, syntax and semantics, and provides examples. He also discusses the implementation of the list framework using generics in the next class.
- Uploaded on | 4 Views
- linarussell
About Comp 212 Lecture 18: Java Generics
PowerPoint presentation about 'Comp 212 Lecture 18: Java Generics'. This presentation describes the topic on In this lecture, Anupam Chanda covers Java generics, including parameterized classes and methods, type safety, syntax and semantics, and provides examples. He also discusses the implementation of the list framework using generics in the next class.. The key topics included in this slideshow are Comp 212, Lecture 18, Java generics, parameterized classes, type safety, syntax, semantics, implementation, list framework,. Download this presentation absolutely free.
Presentation Transcript
1. Comp 212: Intermediate Programming Lecture 18 Java Generics Comp 212: Intermediate Programming Lecture 18 Java Generics By: Anupam Chanda By: Anupam Chanda
2. 2 Todays Lecture Todays Lecture Java generics Java generics Parameterized classes and methods Parameterized classes and methods Compiler provides type safety Compiler provides type safety Syntax and semantics Syntax and semantics Examples Examples Generics-based implementation of the list framework next class Generics-based implementation of the list framework next class
3. 3 Outline Outline Motivation Parameterized classes Parameterized classes Parameterized methods Parameterized methods Upper bounded wildcards Upper bounded wildcards Lower bounded wildcards Lower bounded wildcards Unbounded wildcards Unbounded wildcards
4. 4 public class OldBox { Object data; public OldBox(Object data) { this .data = data; } public Object getData() { return data; } } OldBox intBox = new OldBox(42); int x = (Integer) intBox.getData(); OldBox strBox = new OldBox(Hi); String s = (String) strBox.getData(); int y = (Integer) strBox.getData(); intBox = strBox; ClassCastException! Compiles but fails at runtime Cast Exceptions at Runtime Cast Exceptions at Runtime
5. 5 public class IntBox { Integer data; public IntBox(Integer data) { this .data = data; } public Integer getData() { return data; } } public class StrBox { String data; public StrBox(String data) { this .data = data; } public String getData() { return data; } } IntBox intBox = new IntBox(42); int x = intBox.getData(); StrBox strBox = new StrBox(Hi); String s = strBox.getData(); int y = (Integer) strBox.getData(); intBox = strBox; Errors caught by compiler public class FooBox { Foo data; public FooBox(Foo data) { this .data = data; } public Foo getData() { return data; } } Infinite many classes possible Nave Solution Nave Solution
6. 6 Passing Parameters to Methods: An Analogy Passing Parameters to Methods: An Analogy public abstract class Sum { public static int sum_0_1() { return (0+1); } public static int sum_15_22() { return (15+22); } } public class Main { public static void main(String[] nu) { int j = Sum.sum_0_1(); int k = Sum.sum_15_22(); } } Bad infinite many methods public abstract class NewSum { public static int sum( int m, int n) { return (m+n); } } public class NewMain { public static void main(String[] nu) { int j = NewSum.sum(0,1); int k = NewSum.sum(15,22); } } Pass parameters to methods Methods accept parameters
7. 7 Java Generics: Key Idea Java Generics: Key Idea Parameterize type definitions Parameterize type definitions Parameterized classes and methods Parameterized classes and methods Provide type safety Provide type safety Compiler performs type checking Compiler performs type checking Prevent runtime cast errors Prevent runtime cast errors
8. 8 Parameterized Classes Parameterized Classes public class OldBox { Object data; public OldBox(Object data) { this .data = data; } public Object getData() { return data; } } We want the box to hold a specific class abstractly represented Object does not work as we have seen earlier Solution parameterize the class definition public class Box
9. 9 How to Use Parameterized Classes How to Use Parameterized Classes public class Box
10. 10 When to Use Parameterized Classes When to Use Parameterized Classes Particularly useful for container classes Particularly useful for container classes Containers hold but do not process data Containers hold but do not process data All collections framework classes in Java 5.0 defined using generics All collections framework classes in Java 5.0 defined using generics See the Java 5.0 API documentation See the Java 5.0 API documentation
11. 11 Parameterized Classes: Syntax Note Parameterized Classes: Syntax Note A class can have multiple parameters, e.g: public class Stuff { } Subclassing parameterized classes allowed, e.g: /* Extending a particular type */ class IntBox extends Box
12. 12 Parameterized Classes in Methods Parameterized Classes in Methods A parameterized class is a type just like any other class. It can be used in method input types and return types, e.g: Box
13. 13 So Far So Far Type safety violations Type safety violations Using casts Using casts Parameterized classes solve this problem Parameterized classes solve this problem Provide type safety Provide type safety Enforced by the compiler Enforced by the compiler Particularly useful for container classes Particularly useful for container classes A parameterized class is another type A parameterized class is another type Next bounded parameterized classes Next bounded parameterized classes
14. 14 Bounded Parameterized Types Bounded Parameterized Types Sometimes we want restricted parameterization of classes. We want a box, called MathBox that holds only Number objects. We cant use Box
15. 15 Bounded Parameterized Types (Contd.) Bounded Parameterized Types (Contd.) public class MathBox
16. 16 Bounded Parameterized Types (Contd.) Bounded Parameterized Types (Contd.) Inside a parameterized class, the type parameter serves as a valid type. So the following is valid. public class OuterClass
17. 17 Bounded Parameterized Types (Contd.) Bounded Parameterized Types (Contd.) Java allows multiple inheritance in the form of implementing multiple interfaces. So multiple bounds may be necessary to specify a type parameter. The following syntax is used then:
18. 18 So Far So Far Parameterized classes Parameterized classes Bounded parameterized types Bounded parameterized types To restrict parameter types To restrict parameter types Next parameterized methods Next parameterized methods
19. 19 Parameterized Methods Parameterized Methods Consider the following class: public class Foo { //Foo is not parameterized public
20. 20 Use of Parameterized Methods Use of Parameterized Methods Adding type safety to methods that operate on different types Adding type safety to methods that operate on different types Return type dependent on input type Return type dependent on input type
21. 21 So Far So Far Parameterized classes Parameterized classes Bounded parameterized types Bounded parameterized types Parameterized methods Parameterized methods Next wildcards Next wildcards Bounded Bounded Upper Upper Lower Lower Unbounded Unbounded
22. 22 Upper Bounded Wildcards in Parameterized Types Upper Bounded Wildcards in Parameterized Types We start to run into some new issues when we do some things that seem normal. For instance, the following seems reasonable: Box
23. 23 Upper Bounded Wildcards in Parameterized Types (Contd.) Upper Bounded Wildcards in Parameterized Types (Contd.) public class Box
24. 24 Lower Bounded Wildcards in Parameterized Types Lower Bounded Wildcards in Parameterized Types Suppose we want to write copyTo() that copies data in the opposite direction of copyFrom() . copyTo() copies data from the host object to the given object. This can be done as: public void copyTo(Box
25. 25 Unbounded Wildcards Unbounded Wildcards Use unbounded wildcards when any type parameter works. > is used to specify unbounded wildcards. The following are legal statements. Box> b1 = new Box
26. 26 Conclusions Conclusions Java generics Java generics Parameterized classes and methods Parameterized classes and methods Type safety Type safety Syntax and semantics through examples Syntax and semantics through examples Links to tutorials on the lecture page Links to tutorials on the lecture page Generics-based implementation of the list framework next class Generics-based implementation of the list framework next class