Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.

Similar presentations


Presentation on theme: "OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes."— Presentation transcript:

1 OOP Tirgul 10

2 What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes  Generic Recursive Definitions  Erasure Consequences 2

3 Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

4  A Simple Car hierarchy: Car FordAudi Generics - Motivation

5  Some cars have plates with digits (we’ll treat these as numbers):  And some cars have plates with letters AND digits (we’ll treat these as strings): Generics - Motivation  How can we reflect this in code?

6  Using generics is equivalent to writing a “family” of classes that differ in internal type (In C++ generics are called class Templates). Using Generics to Parameterize Data Types Car FordAudi

7 Generic classes as class “templates” Class Name

8 Generic classes as class “templates” Type Parameter

9 Generic classes as class “templates” So this is the template, but where do we define the specific classes? On instantiation.

10 Generic classes as class “templates” Prints: 12346

11 Generic classes as class “templates” Prints: 123451

12 Generics with Collections  Naturally, the Collections library uses Generics to allow collections of different types: Two levels of generic-ness

13 E E E E Using raw types  What if we instantiate MyArray without a type parameter? T is being set to Object. We will get a warning.

14 Type Safety Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

15 Motivation – Type Safe  A type safe program is a program in which type errors are compile-time errors and not run-time errors String a = new Integer(); Object i = new Integer(5); String b = (String)i; Compile-time type error Run-time type error

16 Motivation – Type Safe  Therefore: Compile-time type error Run-time type error

17  Cannot instantiate generic types with primitive types:  Cannot create instances of type parameters:  Cannot create arrays of a generic class (as it can break the type-safety): Few Restrictions

18 Bounded Type Parameters Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

19 Bounded Type Parameters  What functions the parameter type E has? Compilation Error Object’s functions only

20 Bounded Type Parameters  So what if we want to use a specific member or method of a parameterized type?

21 Bounded Type Parameters Upper bounded  So what if we want to use a specific member or method of a parameterized type?

22 Bounded Type Parameters  Recall that we can do the same with wildcards: class Farmer { public void farmSong(Collection farm) { for (Animal cuteAnimal : farm) { cuteAnimal.makeSound(); } An upper bounded wildcard

23 Generic Methods Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

24 Generic Static Methods  Static variables and methods are shared among all instances of a given class.  Therefore, It is illegal to refer to the type parameters of a type declaration in a static method, or in the declaration of a static variable

25 Generic Methods  However, static and non-static methods can also be generic in-themselves: T is the method’s generic type!

26 Generics and Inner Classes Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

27 Generics and Inner Classes  Remember, the type parameter (,, etc...) only gets its value when an instance is created. Compile Error: A static inner class is not bound to an instance!

28 Generics and Inner Classes  However, we can parameterize the static inner class separately: U U

29 Generic Recursive Definitions Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

30 Generic Recursive Definitions  java.util.Comparable is a java interface representing the ability of an object to compare itself to another object (usually of the same type, but not necessarily).  We would like this line: to result in a compile error. new Car(“Audi”).compareTo(“Mush”); Many methods/classes “want” objects to be comparable to other objects of the same class.

31 Generic Recursive Definitions  The Comparable Interface source code:  Which type will we choose for the following?

32 Generic Recursive Definitions  What if our class is itself generic?  No problem: class MyClass implements Comparable > { //… } class MyClass { //… }

33 Erasure Consequences Generics – A Reminder Type Safety Bounded Type Parameters Generic Methods Generics and Inner Classes Generic Recursive Definitions Erasure Consequences

34  Recall that Generics are implemented in Java using erasure:  The compiler erases all generic type references, using casting where needed.  This has consequences:  No T myT = new T(); T[] myTarray = new T[5]; instanceof T

35 Erasure Consequences  First no-no: class Car { public T getPlate() { return new T(); } Cannot instantiate the type T

36 Erasure Consequences  Second no-no: class Triplet { private T[] triplet; public Triptlet() { triplet = new T[3]; } Cannot create a generic array of T

37 Erasure Consequences  Second no-no: class Wrapper { private T content; public boolean sameTypeContent(Object other) { return (other instanceof T); } Cannot perform instanceof check against type parameter T. Use its erasure Object instead since further generic type information will be erased at runtime


Download ppt "OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes."

Similar presentations


Ads by Google