Professional Documents
Culture Documents
Outline
1. Upcasting and Downcasting
OBJECT-ORIENTED LANGUAGE AND THEORY
2. Static and dynamic bindings
8. POLYMORPHISM
3. Polymophism
4. Generic programming
1 2
3 4
• explicitly cast
• e.g. int => short
• (short)
3 4
1
5 6
5 6
7 8
7 8
2
9 10
9 10
11
4. Generic programming
11 12
3
14
13 14
15 16
15 16
4
17 18
17 18
19 20
Outline 3. Polymorphism
1. Upcasting and Downcasting • Polymorphism: multiple ways of
performance, of existance
2. Static and dynamic bindings • Polymorphism in OOP
3. Polymorphism • Method polymorphism:
• Methods with the same name, only difference in
4. Generic programming argument lists => method overloading
• Object polymorphism
• Multiple types: A single object to represent multiple
different types (upcasting and downcasting)
• Multiple implementations/behaviors: A
single interface to objects of different types
(upcasting+overriding – dynamic binding)
19 20
5
21 22
21 22
24
• interface TVInterface {
Why Polymorphism? public void turnOn();
public void volumnUp(int steps);
• The ability to hide many different implementations behind …
a single interface • }
• class TVA implements TVInterface {
public void turnOn() { … }
…
• }
• class TVB implements TVInterface {…}
Manufacturer B
Manufacturer A Manufacturer C • class TVC implements TVInterface {…}
• class RemoteControl {
TVInterface tva = new TVA(); tva.turnOn(); tva.volumnUp(2);
TVInterface tvb = new TVB(); tvb.turnOn(); tvb.volumnUp(2);
TVInterface tvc = new TVC(); tvc.turnOn(); tvc.volumnUp(2);
• }
Remote Control
23 24
6
26
25 26
27 28
Writing
Writing
Operator instanceof Static methods public class Writer {
Writing
Writing book
public static void write() {
public class Employee extends Person {} • Static methods in Java are }
System.out.println("Writing");
public doSomething(Person e) { hide the superclass public class Programmer extends Writer {
if (e instanceof Employee) {... method instead of public static void write() {
System.out.println("Writing code");
overriding it. }
} else if (e instanceof Student) {... ){ public static void main(String[] args) {
• Static methods are not Writer w = new Programmer();
} else {...} polymorphic. At the w.write();
27 28
7
29 30
class Writer {
2. Static and dynamic bindings
public static void write() {
System.out.println("Writing"); 3. Polymophism
}
} 4. Generic programming
public class Author extends Writer {
public static void main(String[] args) {
Author a = new Author();
a.write();
}
}
29 30
31 32
31 32
8
33 34
33 34
35
}
public int getNumber(){return number;}
}
public class EqualsMethod2 {
public static void main(String[] args) {
MyValue v1 = new MyValue(100);
MyValue v2 = new MyValue(100);
System.out.println(v1.equals(v2));
System.out.println(v1==v2);
}
}
35 36
9
37 38
class MyValue {
int i; Example: Java 1.5: Template
public boolean equals(Object obj) {
return (this.i == ((MyValue) obj).i);
}
}
public class EqualsMethod2 {
public static void main(String[] args) {
MyValue v1 = new MyValue(); • Without Template
MyValue v2 = new MyValue(); List myList = new LinkedList();
v1.i = v2.i = 100; myList.add(new Integer(0));
System.out.println(v1.equals(v2)); Integer x = (Integer)
System.out.println(v1==v2); myList.iterator().next();
}
}
37 38
39 40
Example: Java 1.5: Template (2) 4.1. Java generic data structure
• Collection: a collection of objects
• Using Template:
• List: a collection of objects that are sequential,
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: Collection of key-value pairs (key is unique)
• Linking objects in this set to other sets as a dictionary/a
//myList.add(new Long(0)); à Compile error
telephone book.
39 40
10
41 42
a. Interface of Collection
• Specifies basic interface for
manipulating a set of objects
• Add to collection
• Remove from collection Collection,
• Check if existing
• Contains methods to manipulate
Set and
individual objects or a set of objects List
• Provide methods to traverse objects
in a repeatable collection and
convert a collection to an array
41 42
43 44
43 44
11
45 46
Iterator i = c.iterator();
while (i.hasNext()) {
Object o = i.next();
// Process this object
}
45 46
12
49 50
51 52
13
53 54
53 54
55 56
55 56
14
59 59 60 60
• 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.
59 60
15