Advertisement
vvccs

E5_Shape&Rectangle

Nov 8th, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. class Shape {
  2.     public double getArea() {
  3.         // Default implementation for the base class, return 0.
  4.         return 0;
  5.     }
  6. }
  7.  
  8. class Rectangle extends Shape {
  9.     private double length;
  10.     private double width;
  11.  
  12.     public Rectangle(double length, double width) {
  13.         this.length = length;
  14.         this.width = width;
  15.     }
  16.  
  17.     @Override
  18.     public double getArea() {
  19.         return length * width; // Calculate the area of the rectangle
  20.     }
  21. }
  22.  
  23. public class Main {
  24.     public static void main(String[] args) {
  25.         Rectangle rectangle = new Rectangle(5.0, 3.0);
  26.         System.out.println("Area of the rectangle: " + rectangle.getArea());
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement