Question: In this activity, you are required to implement a simple shape hierarchy in Java using inheritance and polymorphism concepts. The goal is to calculate and
In this activity, you are required to implement a simple shape hierarchy in Java using inheritance and polymorphism concepts. The goal is to calculate and display the area of different shapes using inheritance ar polymorphism.
Instructions:
Shape Class:
Create a class named Shape with the following methods:
calculateArea : Returns to be overridden by subclasses
display: Prints "Area: followed by the calculated area.
Circle Class extends Shape:
Create a subclass named Circle that extends Shape.
Add a private attribute radius and a constructor that initializes it
Create another subclass named Rectang le that extends Shape.
Add private attributes length and width, and a constructor that initializes them.
Override the calcu lateArea method to calculate the area of a rectangle using the formula length wi
Main Class:
In the Main class, create instances of Circ le and Rectang le using sample dimensions.
Call the display method on each instance to print the calculated areas.
Testing:
Compile and run your program to ensure that it produces the correct output, displaying the areas of the circle rectangle.
Reply
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
