Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism Giuseppe Attardi Antonio Cisternino.

Similar presentations


Presentation on theme: "Polymorphism Giuseppe Attardi Antonio Cisternino."— Presentation transcript:

1 Polymorphism Giuseppe Attardi Antonio Cisternino

2 Generic programming Code reuse is clearly a good idea Code reuse is clearly a good idea Often an algorithm can be applicable to many objects Often an algorithm can be applicable to many objects Goal is to avoid rewriting as much as possible Goal is to avoid rewriting as much as possible Types introduce great benefits but also may limit code reuse: Types introduce great benefits but also may limit code reuse: int sqr(int i, int j) { return i*j; } double sqr(double i, double j) {return i*j; } The notion of sqr is unique but we must define it twice because of types The notion of sqr is unique but we must define it twice because of types Languages offer mechanisms to address this problem Languages offer mechanisms to address this problem

3 Polymorphism The ability of associate more than one meaning to a name in a program The ability of associate more than one meaning to a name in a program We have already seen two kinds of polymorphism: We have already seen two kinds of polymorphism: –Subtype/inclusion (inheritance) –Overloading Polymorphism is the fundamental mechanism for generic programming Polymorphism is the fundamental mechanism for generic programming There are other kinds of polymorphism There are other kinds of polymorphism

4 Classification of Polymorphism Polymorphism Universal Ad hoc Parametric Inclusion Overloading Coercion

5 Universal vs. ad hoc polymorphism With overloading an implementation for each signature is required With overloading an implementation for each signature is required We provide ad hoc solutions for different objects We provide ad hoc solutions for different objects Inheritance instead allows defining algorithms that operate on all classes of objects that inherit from a given class Inheritance instead allows defining algorithms that operate on all classes of objects that inherit from a given class In this case a single (universal) solution applies to different objects In this case a single (universal) solution applies to different objects

6 Implementing Polymorphism Dynamic method dispatch Dynamic method dispatch C++ adds a v-table to each object from a class having virtual methods C++ adds a v-table to each object from a class having virtual methods

7 Containers Example: Java Vector Example: Java Vector Vector v = new Vector(); v.addElement("Pippo"); v.addElement(new Integer(2)); Signature of addElement: Signature of addElement: void addElement(Object x); The argument is of type Object because the container may contain any type of object The argument is of type Object because the container may contain any type of object

8 Problem with containers Inserting an object in a vector we loose type information Inserting an object in a vector we loose type information In our example we implicitly upcast from String to Object: In our example we implicitly upcast from String to Object: v.addElement("Pippo"); Extracting the second element with the wrong cast produces a runtime error: Extracting the second element with the wrong cast produces a runtime error: Integer i = (Integer)v.elementAt(0);

9 Weakest constraint programming Where do we assume something about objects that we manipulate? Where do we assume something about objects that we manipulate? class Vector { Object[] v; int size; public Vector() { v = new Object[15]; size = 0; } public addElement(Object e) { if (size == v.length) { Object[] w = new Object[](2 * size); w.copy(v, 0, size); v = w; } v[size++] = e; }} We assume only assignment operations and arrays: operation available on all objects We assume only assignment operations and arrays: operation available on all objects

10 Can we sort our vector? How to add a method for sorting a vector? How to add a method for sorting a vector? We do not have enough information on our objects: no comparison operation is available We do not have enough information on our objects: no comparison operation is available Our vector is too generic! Our vector is too generic! Two solutions: Two solutions: –accept only objects that implement an interface (i.e. IComparable) that exposes a method to compare objects public void addElement(IComparable e) {…} –Pass a functional object: an object which implements an interface for comparing Object instances (i.e. IComparator) public void Sort(IComparator c) {…} interface IComparator { int compare(Object x, Object y); }

11 Abstract as much as possible! To express generic code with subtype polymorphism we should abstract the essence of the operations required on the objects we want to manipulate To express generic code with subtype polymorphism we should abstract the essence of the operations required on the objects we want to manipulate Risk is over-abstraction: once defined our vector we can’t easily add a sort method Risk is over-abstraction: once defined our vector we can’t easily add a sort method Another issue: inheritance relies on explicit annotation of our types and changes are hard to perform Another issue: inheritance relies on explicit annotation of our types and changes are hard to perform

12 Iterating over a collection A common programming pattern is to enumerate the elements of a collection A common programming pattern is to enumerate the elements of a collection It doesn’t really matter how the collection is organized It doesn’t really matter how the collection is organized We can implement a class per collection type whose objects enumerates the elements. We can implement a class per collection type whose objects enumerates the elements. Example: Example: Enumeration elements() { return ???; } void printCollection(Enumeration e) { while (e = hasMoreElements()) { Object o = e.nextElement(); System.out.println(o); }} Interface

13 Question Which class implements method elements? Which class implements method elements? –Class Vector –Use overloading and singleton class Enumeration { static Enumeration getEnumeration(Vector v){ return v.elements(); } // Other collections’ enumerators } Thus we can add enumerators to existing collections Thus we can add enumerators to existing collections

14 Enumerator for Vector class VectorEnum implements Enumeration { int idx; int idx; Vector v; Vector v; bool hasMoreElements() { idx < v.size(); } bool hasMoreElements() { idx < v.size(); } Object nextElement() { Object nextElement() { return v.elementAt(idx++); return v.elementAt(idx++); } VectorEnum(Vector v) { VectorEnum(Vector v) { idx = 0; idx = 0; this.v = v; // why it is not copied? this.v = v; // why it is not copied? }}

15 Is the enumerator up to date? To ensure that the enumerator is consistent the vector should be copied into the enumerator To ensure that the enumerator is consistent the vector should be copied into the enumerator This isn’t reasonable: memory wasted and we iterate on a different vector! This isn’t reasonable: memory wasted and we iterate on a different vector! There is no way to ensure that the enumerator is consistent with the vector There is no way to ensure that the enumerator is consistent with the vector Possible solution: introduce a “version” of the vector Possible solution: introduce a “version” of the vector Each time the vector is modified the version is incremented Each time the vector is modified the version is incremented Enumerator compares the version of the vector with the one at time of creation Enumerator compares the version of the vector with the one at time of creation

16 Event handling in GUI Before Java 1.1 OO GUI frameworks were based on sub-typing Before Java 1.1 OO GUI frameworks were based on sub-typing GUI can be easily described using generic programming: buttons are a subtype of control which is a special window GUI can be easily described using generic programming: buttons are a subtype of control which is a special window Containers of graphical widgets operates on controls, irrespective of their types Containers of graphical widgets operates on controls, irrespective of their types Event dispatching and handling is dealt by virtual methods Event dispatching and handling is dealt by virtual methods –hence by default is delegated to the super-type

17 Java AWT Event Model class Component { int x, y; int x, y; bool handleEvent(Event e); bool handleEvent(Event e);} class Button extends Component { String text; String text; bool handleEvent(Event e) { } bool handleEvent(Event e) { } …} class Window extends Component { … } class Frame extends Window { … }

18 Event handling class MyButton extends Button { boolean handleEvent(Event e) { switch (e.type) { case Event.MOUSE_UP: … return true; // Event handled! return true; // Event handled!}default: return super.handleEvent(e); return super.handleEvent(e);}}

19 Limits of AWT Event Model Generic programming in this case is quite elegant but inefficient Generic programming in this case is quite elegant but inefficient Propagation of events to a number of handlers, mostly useless Propagation of events to a number of handlers, mostly useless Proliferation of classes: one for each object with different behavior Proliferation of classes: one for each object with different behavior

20 Alternative Event Delegation model Event Delegation model Observer Pattern (aka publish/subscribe) Observer Pattern (aka publish/subscribe) Observable has set of registered observers Observable has set of registered observers Observable notifies its observers when its state changes Observable notifies its observers when its state changes Handling performed by objects that provide a Listener interface (aka callback, delegate) Handling performed by objects that provide a Listener interface (aka callback, delegate)

21 Java JDBC Java DataBase Connectivity is a specification from Sun for accessing databases in Java Java DataBase Connectivity is a specification from Sun for accessing databases in Java Interesting example of generic programming Interesting example of generic programming It implements a driver architecture exploiting the mechanisms of JVM It implements a driver architecture exploiting the mechanisms of JVM

22 Overall architecture The java.sql package exposes only interfaces The java.sql package exposes only interfaces The only class is DriverManager The only class is DriverManager Using the class constructor a driver register itself with the DriverManager Using the class constructor a driver register itself with the DriverManager The programmer performs the following steps: The programmer performs the following steps: –Load the database driver (a Java class) –Create a connection to a database (using DriverManager) –Obtain a Statement and execute the query –Enumerate the rows using a ResultSet

23 JDBC example Class.forName("…"); // Load the driver Connection c = DriverManager.getConnection("…"); Statement s = c.createStatement(); ResultSet r = s.executeQuery("select …"); while (r.hasNext()) { // Value of second column as String // Value of second column as String String s = r.getString(2); String s = r.getString(2);}

24 Question Statement is Class or Interface? Statement is Class or Interface? Connection, Statement are Interfaces Connection, Statement are Interfaces Through the DriverManager the program obtains an object of unknown type which implements the Connection interface Through the DriverManager the program obtains an object of unknown type which implements the Connection interface The same applies to all the other interfaces: through the connection an object implementing Statement is obtained, and so on and so forth The same applies to all the other interfaces: through the connection an object implementing Statement is obtained, and so on and so forth


Download ppt "Polymorphism Giuseppe Attardi Antonio Cisternino."

Similar presentations


Ads by Google