Java abstract classes and enums

An abstract class cannot be instantiated. It describes what its subclasses share and declares abstract methods that every concrete subclass must implement. Code written against the abstract type, such as a method that takes any Shape, works for all subclasses through polymorphism.

Java
import java.util.Locale;

class Main {
  static abstract class Shape {
    abstract double area();

    String describe() {
      return getClass().getSimpleName() + " area " + String.format(Locale.ROOT, "%.2f", area());
    }
  }

  static class Circle extends Shape {
    private final double radius;

    Circle(double radius) {
      this.radius = radius;
    }

    double area() {
      return Math.PI * radius * radius;
    }
  }

  static class Rect extends Shape {
    private final double width;
    private final double height;

    Rect(double width, double height) {
      this.width = width;
      this.height = height;
    }

    double area() {
      return width * height;
    }
  }

  enum Level {
    LOW(1), MEDIUM(5), HIGH(10);

    private final int weight;

    Level(int weight) {
      this.weight = weight;
    }

    int weight() {
      return weight;
    }
  }

  public static void main(String[] args) {
    Shape[] shapes = {new Circle(1.5), new Rect(2, 3.5)};
    for (Shape shape : shapes) {
      System.out.println(shape.describe());
    }
    for (Level level : Level.values()) {
      System.out.println(level + " " + level.weight());
    }
  }
}
Read the full lesson