Runtime polymorphism
Figure 515. Polymorphism: Iteration over
Object
instances | Code | final Object[] instances = {
new String("Test"),
LocalTime.of(16, 30),
new Circle(2.0, 3.0, 7)
};
print(instances); |
|---|---|
| Result | Test 16:30 (2.0|3.0), radius=7.0 |
| Code | final Object[] instances = {
new String("Test"),
LocalTime.of(16, 30),
new Circle(2.0, 3.0, 7)
};
print(instances);static void printMetaInfo(Object[] objects) {
for (final Object o: objects) {
IO.println(o.getClass().getName());
}
} |
|---|---|
| Result | java.lang.String java.time.LocalTime de.hdm_stuttgart.mi.sd1.Circle |
Steps during run time:
-
Determine object's type.
-
Select corresponding method in inheritance hierarchy.
-
Execute method.
final Shape circle = new Circle(2.0, 3.0, 7.0); circle.move(5.0, 2.0);
Which method is being executed?
|
|
public abstract class Shape {
...
public double getArea() {
return ??? ;
}
}public abstract class Shape { ... // No implementation body {...} abstract public double getArea(); } |
|||
public class Rectangle extends Shape {
...
private int width, height;
@Override public double getArea() {
return width * height;
}
} |
public class Circle extends Shape {
...
private int radius;
@Override public double getArea() {
return Math.PI * radius * radius;
}
} |
||
| Code | final Shape[] shapes = {
new Rectangle(0, 0, 2, 3),
new Circle(0, 0, 1)};
}
printAreas(shapes);static void printAreas(Shape[] shapes) {
for (Shape shape : shapes) {
IO.println(shape.getArea());
}
} |
|---|---|
| Result | Area:6.0 Area:3.141592653589793 |
public abstract class Shape {
...
public final double getArea(){
if (this instanceof Rectangle rectangle) {
return rectangle.width * rectangle.height;
} else if (this instanceof Circle circle) {
return circle.radius * circle.radius * Math.PI;
} else {
return 0; // Unreachable
}
} |
|||
public class Rectangle extends Shape { protected int width, height; ... } |
public class Circle extends Shape { protected int radius; ... } |
||
