Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started