Escolar Documentos
Profissional Documentos
Cultura Documentos
CT038-3-2 OODJ
Polymorphism
Learning outcomes
At the end of this lecture you should be
able to:
Understand the implementation of
polymorphism.
CT038-3-2 OODJ
Polymorphism
CT038-3-2 OODJ
Polymorphism
Polymorphism
Treat objects in same class hierarchy as if all
superclass
Abstract class
Common functionality
CT038-3-2 OODJ
Polymorphism
Concrete classes
Can be instantiated
Implement every method they declare
Provide specifics
CT038-3-2 OODJ
Polymorphism
Abstract methods
No implementation, must be overridden
CT038-3-2 OODJ
Polymorphism
Application example
Abstract class Shape
Declares draw as abstract method
CT038-3-2 OODJ
Polymorphism
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CT038-3-2 OODJ
Polymorphism
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CT038-3-2 OODJ
Polymorphism
CT038-3-2 OODJ
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Polymorphism
CT038-3-2 OODJ
Polymorphism
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CT038-3-2 OODJ
// return radius
public double getRadius()
{
return radius;
}
// calculate and return diameter
public double getDiameter()
{
return 2 * getRadius();
}
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
// override method getArea to return Circle area
public double getArea()
{
return Math.PI * getRadius() * getRadius();
}
Polymorphism
CT038-3-2 OODJ
Polymorphism
Abstract methods
No implementation, must be overridden
CT038-3-2 OODJ
Polymorphism
Application example
Abstract class Shape
Declares draw as abstract method
CT038-3-2 OODJ
Polymorphism
final classes
Cannot be superclasses
Methods in final classes are implicitly
final
e.g., class String
CT038-3-2 OODJ
Polymorphism
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CT038-3-2 OODJ
Polymorphism
CT038-3-2 OODJ
Polymorphism