We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
Write a Java program that demonstrates the use of an abstract class and abstract methods with
inheritance and polymorphism. Follow these guidelines:
1. Create an abstract class named Shape that contains:
o An abstract method calculateArea() that returns a double value representing the area of the shape. o A concrete method displayArea() that prints the area by calling the calculateArea() method. 2. Create two subclasses, Circle and Rectangle, that each extend Shape: o Circle: Has a private field radius. Includes a constructor to initialize the radius. Implements the calculateArea() method using the formula for the area of a circle: π×radius2\pi \times \text{radius}^2π×radius2. o Rectangle: Has private fields length and width. Includes a constructor to initialize length and width. Implements the calculateArea() method using the formula for the area of a rectangle: length×width\text{length} \times \text{width}length×width. 3. In the main method, prompt the user to select a shape type: o Ask the user to enter 1 for Circle or 2 for Rectangle. o Based on the user's choice: If the user selects Circle, ask for the radius and create a Circle object. If the user selects Rectangle, ask for the length and width and create a Rectangle object. o After creating the appropriate object, call displayArea() on it to print the calculated area. 4. Use Scanner for user input, and handle invalid choices by displaying an error message.