11
16 - Generics

16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

Embed Size (px)

Citation preview

Page 1: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

16 - Generics

Page 2: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

2NOEA 2009Java-kursus – Generics

”Generic” Programming in C#/Java(as it was until Summer 2005)

• All classes inherit from Object• So we can apply polymorphism and use Object as static

type for elements in containers• For instance: Object[ ] data

– this array may take any object as element– This approach is well known from standard collections as

ArrayList, HashMap etc.

Page 3: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

3NOEA 2009Java-kursus – Generics

Pro and cons

• Pro– heterogeneous collections

• Cons– many type casts– not type safe

• type checking is done runtime when casting– int and other native (value) type must be wrapped. (boxing –

costs runtime overhead)

Is this really an

advantage?

Page 4: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

4NOEA 2009Java-kursus – Generics

Programming Error

• The program is incorrect, but we don’t detect it

• It is preferable, if :• The program doesn’t work correctly, and we are told by

some runtime error (exception) • The compiler rejects the program (compile-time error)

Page 5: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

5NOEA 2009Java-kursus – Generics

Strategy

• Do as much error-checking compile-time as possible:

• Advantages:– Easier to write correct programs– More efficient program execution (better performance)

• Disadvantages:– Restricts programmers creativity (less flexibility)

Is this really a disadvantage?

Page 6: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

6NOEA 2009Java-kursus – Generics

The Idea: Types as Parameters

C#/Java before 2005:ArrayList al = new ArrayList();

Customer c= (Customer)al.get(i);//cast

Instead we want something like:

ArrayList<Customer> al = new ArrayList<Customer>();

Customer= al.get(i);

– The compiler is able to check that only objects with static type Customer is placed in al– So the compiler knows that everything that may come out from al has static type Customer–So static type checking instead of dynamic type checking–Dynamic casting can be avoided (but is not in all implementations)

Type parameter

Page 7: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

7NOEA 2009Java-kursus – Generics

In C#: EmpSeqAppl

Employee a1 = new Employee("Joe", "Programmer", 10000);Employee a = new Employee("Curt", "Senior Programmer", 20000);Employee b = new Employee("Carl", "Programmer", 10000);Employee c = new Employee("Karen", "System Programmer", 13000);Employee d = new Employee("Lisa", "Programmer", 11000);Employee e = new Employee("John", "System Engineer", 9000);string s = "HELLOOOO!";

ArrayListList<Employee> emps = new ArrayList<Employee>();emps.Add(a1);emps.Add(a);emps.Add(b);emps.Add(c);emps.Add(d);emps.Add(e);//emps.Add(s);//COMPILER ERROR!!!!

Page 8: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

8NOEA 2009Java-kursus – Generics

How to write your own generics

using System.Collections.Generic;

public class MyList<T> implements Enumerable, Iterable

T[] list;

public void Add(T o)

public T Get(int i)

Page 9: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

9NOEA 2009Java-kursus – Generics

But everything comes at a price:

– no heterogeneous collections• But that is not so bad...

– more complicated type system– more complicated syntax– one needs a pre-processor, giving

• slower compilation• more mysterious error messages

Page 10: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

10NOEA 2009Java-kursus – Generics

Generics vs. Polymorphism

• Generics:– all elements has the same static type (not object!)– static typing (compile-time), static binding is possible– For instance trying to insert a float to List<int> l yields

an compiler error• Data Structures based on Polymorphism:

– elements may have different types, but all must be subtypes of Object

– Dynamic type-checking (when casting)– Dynamic binding

Page 11: 16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can

11NOEA 2009Java-kursus – Generics

Further reading

• In C#:– http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/

html/csharp_generics.asp– Troelsen, chap. 10

• In Java:– http://java.sun.com/developer/technicalArticles/J2SE/generics/index.html

– Also generics\Java5

• In C++:– http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/

html/vcrefTemplates.asp

– Also generics\cppSTL