Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

What basic rights must attach to at least one class of shares?

Answered: 1 week ago

Question

I receive useful feedback about my performance.

Answered: 1 week ago