Question
Polymorphism in JAVA Define an abstract base class called Shape. The Shape class should have the following members: Protected Field: area, a double used to
Polymorphism in JAVA
Define an abstract base class called Shape. The Shape class should have the following members:
Protected Field: area, a double used to hold the shape's area. Public Methods: getArea. This method should return the value in the member variable area. calcArea. This function should be abstract. toString. Reports the shape's area.
Next, define a class named Circle . It should be derived from the Shape class. It should have the following members: Private Fields: centerX, a long integer used to hold the x coordinate of the circles center. centerY, a long integer used to hold the y coordinate of the circles center. radius, a double used to hold the circle's radius. Public Methods: constructor accepts values for centerX, centerY, and radius. Should call the overridden calcArea function described below. getCenterXreturns the value in centerX. getCenterYreturns the value in centerY. @override calcAreacalculates the area of the circle (area = 3.14159 * radius * radius) and stores the result in the inherited member area. @override toString. Reports the shape's area and displays radius, centerX and centerY.
Next, define a class named Rectangle . It should be derived from the Shape class. It should have the following members:
Private Fields: width, a long integer used to hold the width of the rectangle. length, a long integer used to hold the length of the rectangle. Public Methods: constructoraccepts values for width and length. Should call the overridden calcArea function described below. getWidthreturns the value in width. getLengthreturns the value in length. @override calcAreacalculates the area of the rectangle (area = length * width) and stores the result in the inherited member area. @override toString. Reports the shape's area and displays length and width.
After you have created these classes, create a test class that defines a Circle object and a Rectangle object. Demonstrate that each object properly calculates and reports its area using Polymorphism. In order to demonstrate polymorphic behavior, assign the objects to a Shape array then call their toString methods in a loop.
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