Generic Programming
Read Ch5
1 Outline
• Why do we need generic programming? • Generic class • Generic method • Java interface, generic interface • Java object type and conversion
2 Why do we need generic class?
• IntNode/DoubleNode/ByteNode/Loca onNode/… • IntArrayBag/DoubleArrayBag/ByteArrayBag/Loca onBag/…
• A generic class (e.g., bag, node) can be used for all the above possible bags.
3 Outline
• Why do we need generic programming? • Generic class • Generic method • Java interface, generic interface • Java object type and conversion
4 Generic Class public class ArrayBag
Use a generic class: ArrayBag
5 Something to pay a en on -- constructor public ArrayBag(int ini alCapacity) { if (ini alCapacity < 0) throw new IllegalArgumentExcep on ("The ini alCapacity is nega ve: " + ini alCapacity); data = (E[]) new Object[ini alCapacity]; manyItems = 0; } data = new E[ini alCapacity]; WRONG
6 Something to pay a en on -- elements
• IntArrayBag – Data contains the real value of each element • ArrayBag
• Implementa ons of Equals, countOfOccurences, search, etc. for(int i=0; i Something to pay a en on – remove elements • IntArrayBag – manyItems--; • ArrayBag 8 Node, Linked List • Cont… 9 Node 10 Outline • Why do we need generic programming? • Generic class • Generic method • Java interface, generic interface • Java object type and conversion 11 How to write a general purpose class • Generic programming – Template in C++ – Parameterized types in Design pa erns • Solu on 2: write Generic class 12 Generic method Public T: Type E: Element 13 Generic method Public (3) Used like a class name int i = getFirst (intArray); String str = getFirst(strArray); A compiler can detect Some restric ons (p252) certain type errors. • Cannot create a new array of elements in type T T[] tArray = new T[10]; WRONG • Cannot call a constructor T tElement = new T(); WRONG 14 Outline • Why do we need generic programming? • Generic class • Generic method • Java interface, generic interface • Java object type and conversion 15 Interfaces and iterators • A java interface: primarily a list of related methods that a programmer may want to implement in a single class. • In its most common form, an interface is a group of related methods with empty bodies. • Interface implementa on (use keyword implements) – Public class – If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. • Example: people, student, staff 16 People interface /** * Interface for a person */ public interface People { //Return the name of a person public String getName(); //Return the age of a person public int getAge(); public boolean equals(People p); } 17 /** * Implement the People interface */ public class Staff implements People { private int age; private String name; public Staff(){age=0;name="";} public Staff(int page, String pName){age=page; name=pName;} @Override public String getName() { // return a silly name return "Non sense"; } … } 18 /** * Implements the people interface */ public class Student implements People{ private String name; @Override public String getName() { // return a fixed name return "Alice"; } @Override public int getAge() { // return a fixed name return 0; } … } 19 Interface as type • A method use interface name as type • The actual argument must be a data type that implements the interface 20 public class PeopleTest { public sta c void printPeople(People p){ System.out.println(p.getName()+","+p.getAge()); } public sta c void main(String[] args) { //People p = new People();//Wrong People s = new Student(); People staff = new Staff(); printPeople (s); printPeople (staff); } } 21 Test whether a class implements an interface • Instanceof if(obj instanceof People) … else … 22 Outline • Why do we need generic programming? • Generic class • Generic method • Java interface, generic interface • Java object type and conversion 23 Generic interface • A generic interface: specifies a list of methods, but these methods depend on one or more unspecified classes – Iterator 24 Comparable • Class Loca on implements comparable • Example to use Loca on’s compareTo method. – Different compareTo implementa on 25 public class Loca on implements Comparable