blob: 1235c27a25ee926ff54c4a05a0b416f26ce420e0 (
plain) (
blame)
1
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
28
29
30
31
|
#import "/template.typ": *
#show: A => apply(A)
#set raw(lang: "java-new")
#set par(leading: 0.7em)
#assignment(21)[
Write a program, wherein create an Interface and create two sub-classes implementing the interface to show the functionalities of runtime polymorphism (_a.k.a._ dynamic method dispatch) using them.
]
#scos("RuntimePolymorphismExample")
=== Discussion
#skind[Interfaces, classes and methods implemented in the program]
- Interface `Shape`:
- `void draw()`: Declares a method.
- Class `Circle` (implements `Shape`):
- Implements the `draw()` method from the `Shape` interface.
- Method `void calculateArea()`: Declares a specific method for calculating the area of a circle.
- Class `Square` (implements `Shape`):
- Implements the `draw()` method from the `Shape` interface.
- Method `void calculateArea()`: Declares a specific method for calculating the area of a square.
- Class `RuntimePolymorphismExample`:
- `public static void main(String[])`: Contains the main program demonstrating runtime polymorphism by creating objects of subclasses (`Circle` and `Square`) and calling the `draw()` method dynamically.
#signature()
|