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.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesBuilding class hierarchies
Inheritance
Shape inheritance and area calculation
Report a typo
Sample Input 1:
Square
5
5Sample Output 1:
Square
25.0Sample Input 2:
Box
3
4Sample Output 2:
Box
12.0Write 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
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.