Question: In this exercise, you have a base class Shape with a single method draw ( ) that prints Drawing a shape . Create two subclasses,
In this exercise, you have a base class Shape with a single method draw that prints Drawing a shape
Create two subclasses, Circle and Square, that override the draw method to print Drawing a circle
and Drawing a square respectively.
In the main method, an instance of the Shape class, s which is actually an instance of the Circle, then the
Square subclasses. When we call the draw method on these objects, the correct implementation is
called based on the actual type of the object, this is runtime polymorphism.
OUTPUT
The program must output: Drawing a circle and Drawing a square
The draw method is overridden in the subclasses, and this allows for the program to determine which
method to use at runtime. This is known as runtime polymorphism or dynamic polymorphism, because
at runtime the JVM determines the actual type of the object and calls the corresponding method.
REQUIRED:
Complete the code, using polymophism method overrride with the required output:
Drawing a circle
Drawing a square
Upload your java file with the completed code.
ChExShapePolymorphismastu.java
public class ChExShapePolymorphismastu
public static void mainString args
Shape s new Circle; create Circle object
sdraw;
s new Square; create Square object
sdraw;
class Shape
public void draw
System.out.printlnDrawing a shape";
class Circle
INSERT CODE HERE
class Square
INSERT CODE HERE
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
