1 Generic classes : Construction, speciæcation, veriæcation, and static analysis Baudouin Le Charlier University of Namur Belgium Venise march 2000
2 Plan of the lectures 1. Prerequisites about OO and Java 2. Construction of generic classes 3. Speciæcation and veriæcation of generic classes 4. Static analysis of generic classes èautomatic veriæcationè
3 Generic classes : Some basic notions about object oriented programming and Java èlecture 1è Baudouin Le Charlier University of Namur Belgium Venise march 2000
4 Plan æ About these lectures æ Reusability in OO æ Inheritance in Java æ Class types in Java æ Polymorphism in Java æ Accessibility in Java æ Abstract methods and abstract classes
5 About these lectures æ Non topics í World wide web programming with Java í Graphical interfaces in Java í System programming in Java í Concurrent programming in Java í Data base access in Java í... æ Topics í Reusability of software in Java í Generic classes : simulating genericity by inheritance í Software correctness and veriæcation
6 Reusability in Object Oriented programming æ There is a lot of redundancy in ëclassical" programs. The same algorithm is re-coded many times with very little changes. æ Common examples : sorting and searching algorithms, classical data structures such as linked lists and binary trees. æ The main objective of object oriented programming is to reuse existing code as much as possible. æ Consequence : the coding of algorithms must be as general ègeneric, parametrizedè as possible.
7 Inheritance in Java æ A class is deæned by extending a previously deæned class. æ The new class reuses the members èi.e., æelds, methodsè of the previously existing class. æ The new class may customize èi.e., modify, extend, specialize,... è the behaviour of the old one. æ The members of the old class can be redeæned èi.e., hidden, overriddenè in the new one.
8 Class types in Java æ A class type is the name of a class. æ The class hierarchy deænes an ordering on the types èspecializationè. æ A variable of type T may contain a reference to an instance of any class T' that is more specialized than T. You may write, for instance : T x = new T'èè; T y = x; T' z = new T'èè; T w = z; æ Hence, a very general type is similar to a parametric type.
9 Polymorphism in Java æ Parametric polymorphism You can write a method mèt 1 x 1,..., Tn xnè with very general parameter types T 1,..., Tn. The method can be applied to actual values of any types T' 1,..., T'n that are more specialized èor equal toè T 1,..., Tn. æ Dynamic dispatch Every time you extend a class, you can redeæne any method of it to specialize, extend, reæne,... its behaviour. The suitable version of the method will be determined at run type depending on the actual type of the object. For instance, class Af int mèèf return 1 ;gg class B extends Af int mèèf return 2 ;gg... A x ;...x = new Bèè ;...x.mèè; The last statement returns the value 2.
10 To ensure a clean separation between the speciæcation èof the interfaceè of a class and its implementation, access to the members of a class can be restricted. There are four accessibility levels : æ private Accessible to code of the class only. Pure implementation notion. æ package èdefaultè Accessible to the package of the class. A service available in the package of the class only. æ protected Accessible in the package of the class but also to other classes that extends this èpublicè class provided that this member is needed to complete the implementation. æ public Accessible everywhere. A public service provided by the class. A member cannot be redeæned with a lower accessibility level.
11 Abstract methods and abstract classes æ Abstract classes are classes whose implementation is incomplete. One cannot create instances of an abstract class. Abstract classes ènormallyè contains at least one abstract method. æ Abstract methods are methods whose implementation is not provided. Only the types of the result and parameters are provided èto allow type-checkingè. The actual implementation will be æxed later in the actual èconcreteè classes extending the abstract class. Abstract methods can be used by normal methods. However those methods will only be applied to instances of concrete classes. Thus the abstract methods will be replaced by their implementation in the concrete class thanks to dynamic dispatch.
12 Construction of generic classes : Intuitive presentation èlecture 2è Baudouin Le Charlier University of Namur Belgium Venise march 2000
13 Generic classes æ Intuitively, a generic class is a class that is parametrized on one èmaybe severalè typeèsè T associated with a number of operations op 1,..., op n. æ A generic class can be instantiated to an actual class by specifying an actual type T and actual operations. æ Generic classes are not provided in Java. æ Generic classes can be simulated in Java thanks to other mechanisms èi.e., inheritance, abstract methods, dynamic dispatchè. æ In the second lecture, we explain how this can be done.