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 run-time 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.
Ch81_Ex90_Shape_Polymorphism_1a_stu.java
public class Ch81_Ex90_Shape_Polymorphism_1a_stu
{
public static void main(String[] args)
{
Shape s = new Circle(); // create Circle object
s.draw();
s = new Square(); // create Square object
s.draw();
}
}
class Shape
{
public void draw()
{
System.out.println("Drawing 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!