From fc4d96a9e3e20f8ddf035c16fc32b1effb75481b Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sun, 28 Jan 2024 06:38:55 +0000 Subject: Complete all Java assignments --- java/text/RuntimePolymorphismExample.typ | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 java/text/RuntimePolymorphismExample.typ (limited to 'java/text/RuntimePolymorphismExample.typ') diff --git a/java/text/RuntimePolymorphismExample.typ b/java/text/RuntimePolymorphismExample.typ new file mode 100644 index 0000000..1235c27 --- /dev/null +++ b/java/text/RuntimePolymorphismExample.typ @@ -0,0 +1,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() -- cgit 1.4.1-2-gfad0