Shape inheritance and area calculation

Report a typo

Create a base class called Shape with a constructor that takes the shape's name as a parameter. Then create a derived class called Rectangle that inherits from Shape and adds a constructor which takes the rectangle's length and width as parameters, as well as a method to calculate the rectangle's area. Create an instance of the Rectangle class, passing the name, length, and width to the constructor. Print the rectangle's name and area.

Sample Input 1:

Square
5
5

Sample Output 1:

Square
25.0

Sample Input 2:

Box
3
4

Sample Output 2:

Box
12.0
Write a program in Java 25
import java.util.Scanner;

// Define the base class Shape

// Define the derived class Rectangle

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the rectangle's name, length, and width from the user

// Create an instance of the Rectangle class

// Print the rectangle's name and area
}
}

Create a free account to access the full topic