import [Link].
Scanner;
// Abstract base class
abstract class Shape {
double value1;
double value2;
// Constructor
Shape(double value1, double value2) {
this.value1 = value1;
this.value2 = value2;
// Abstract method
abstract double compute_area();
// Derived class: Triangle
class Triangle extends Shape {
Triangle(double base, double height) {
super(base, height);
@Override
double compute_area() {
return 0.5 * value1 * value2;
// Derived class: Rectangle
class Rectangle extends Shape {
Rectangle(double length, double width) {
super(length, width);
@Override
double compute_area() {
return value1 * value2;
// Main class
public class ShapeAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Shape shape;
[Link]("Choose shape (1. Triangle, 2. Rectangle): ");
int choice = [Link]();
if (choice == 1) {
[Link]("Enter base of triangle: ");
double base = [Link]();
[Link]("Enter height of triangle: ");
double height = [Link]();
shape = new Triangle(base, height);
else if (choice == 2) {
[Link]("Enter length of rectangle: ");
double length = [Link]();
[Link]("Enter width of rectangle: ");
double width = [Link]();
shape = new Rectangle(length, width);
} else {
[Link]("Invalid choice.");
[Link]();
return;
// Dynamic binding in action
[Link]("Area: " + shape.compute_area());
[Link]();