Professional Documents
Culture Documents
Outline: 8. Polymophism
Outline: 8. Polymophism
Outline: 8. Polymophism
vn 2
Outline
1. Upcasting and Downcasting
OBJECT-ORIENTED LANGUAGE AND THEORY
2. Static and dynamic bindings
8. POLYMOPHISM
3. Polymophism
Nguyen Thi Thu Trang 4. Generic programming
trangntt@soict.hust.edu.vn
3 4
1
5 6
7 8
2
9
4. Generic programming
12
3
13 14
15 16
4
17 18
Outline 3. Polymophism
1. Upcasting and Downcasting • Polymorphism: many ways of performance,
of existance
2. Static and dynamic bindings
• Polymorphism in OOP
3. Polymophism • Method polymorphism:
• Methods with the same name, only difference in
4. Generic programming argument lists.
• Object polymorphism
• A single symbol to represent multiple different
types
• A single interface to entities of different types
19 20
5
Why Polymorphism? What Is an Interface?
• The ability to hide many different implementations behind • A declaration of a coherent set of public features
a single interface and obligations
• A contract between providers and consumers of services
Canonical
Elided/Iconic
(Class/Stereotype)
Manufacturer B Representation
Manufacturer A Manufacturer C Representation
(“ball”) Manufacturer A Manufacturer A
<<interface>>
RemoteSensor Manufacturer B
Manufacturer B
Remote Sensor
Manufacturer C Manufacturer C
Remote Control
23 24
6
25 26
27 28
7
29 30
Example: Java using upcasting and Object Recall – equals of a custom class
class MyStack { class MyValue {
... int i;
public void push(Object obj) {...} }
public Object pop() {...} public class EqualsMethod2 {
} public static void main(String[] args) {
public class TestStack{ MyValue v1 = new MyValue();
MyStack s = new MyStack(); MyValue v2 = new MyValue();
Point p = new Point(); v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
Circle c = new Circle();
System.out.println(v1==v2);
s.push(p); s.push(c);
}
Circle c1 = (Circle) s.pop(); }
Point p1 = (Point) s.pop();
}
32
• Without Template
List myList = new LinkedList();
myList.add(new Integer(0));
Integer x = (Integer)
myList.iterator().next();
8
33 34
Example: Java 1.5: Template (2) 4.1. Java generic data structure
• Collection: a set of objects
• Using Template: • List: a collection of objects that are sequencial,
List<Integer> myList = new LinkedList<Integer>(); consecutive and repeatable
myList.add(new Integer(0));
• Set: a collection of objects that are not repeatable
Integer x = myList.iterator().next();
• Map: Set of pairs key-value without repeatation
• Linking objects in this set to other sets as a dictionary/a
//myList.add(new Long(0)); à Error
telephone book.
35 36
a. Interface of Collection
• Specifies basic interface for
manipulating a set of objects
• Add to collection
• Remove from collection Collection,
• Check if existing
Set and
• Contains methods to manipulate
individual objects or a set of objects List
• Provide methods to traverse objects
in a repeatable collection and
convert a collection to an array
9
37 38
39 40
Iterator i = c.iterator();
while (i.hasNext()) {
Object o = i.next();
// Process this object
}
10
41 42
43 44
11
45 46
47 48
12
49 50
53 53 54 54
• There are no functions with the same name • Is an execution of service that is requested in the message
• Is a piece of code to be executed in order to respond to a message
• Message passing
sent to an object
• Request a service from an object and the object will
decide what to do
• Different objects will have different re-
actions/behaviors for a message.
13