1. Write a Java program to create a class called Shape with a method called getArea().
Create a subclass called
Rectangle that overrides the getArea() method to calculate the area of a rectangle.
public class Shape
{
   public double getArea()
  {
     // Return 0.0 as the default area
     return 0.0;
   }
}
// Define the child class Rectangle that extends Shape
public class Rectangle extends Shape
{
   private double length;
   private double width;
        // Define the constructor that takes length and width as parameters
        public Rectangle(double length, double width)
    {
            this.length = length;
            this.width = width;
        }
     // Use the @Override annotation to indicate that this method overrides a method in the superclass
     @Override
     // Define the getArea method that returns a double
     public double getArea()
    {
            return length * width;
        }
}
public class Main
{
  public static void main(String[] args) {
    // Create an instance of Rectangle with length 3.0 and width 10.0
    Rectangle rectangle = new Rectangle(3.0, 10.0);
    // Call the getArea method on the rectangle instance and store the result in the area variable
    double area = rectangle.getArea();
    // Print the area of the rectangle to the console
    System.out.println("The area of the rectangle is: " + area);
  }
}
Output:
The area of the rectangle is: 30.0
2. Write a Java program to create a class called Employee with methods called work() and getSalary()
                                                                                                                  1
public class Employee
{
     private int salary;
     public Employee(int salary)
   {
     this.salary = salary;
  }
        // Method to simulate the employee working
        public void work()
    {
            // Print a message indicating the employee is working
            System.out.println("working as an employee!");
        }
     // Getter method to retrieve the salary of the employee
     public int getSalary()
    {
        return salary;
     }
}
public class HRManager extends Employee
{
    public HRManager(int salary)
  {
     // Call the parent class constructor with the salary
     super(salary);
  }
        // Overridden method to simulate the HRManager working
        public void work()
    {
            // Print a message indicating the HRManager is managing employees
            System.out.println("\nManaging employees");
        }
        public void addEmployee()
{
                System.out.println("\nAdding new employee!");
        }
}
public class Main
{
    public static void main(String[] args)
  {
      // Create an Employee object with a salary of 40000
      Employee emp = new Employee(40000);
            // Create an HRManager object with a salary of 70000
            HRManager mgr = new HRManager(70000);
            // Call the work method on the Employee object
            emp.work();
                                                                                2
        // Print the salary of the Employee object
        System.out.println("Employee salary: " + emp.getSalary());
        // Call the work method on the HRManager object
        mgr.work();
        // Print the salary of the HRManager object
        System.out.println("Manager salary: " + mgr.getSalary());
        // Call the addEmployee method on the HRManager object
        mgr.addEmployee();
    }
}
Output:
working as an employee!
Employee salary: 40000
Managing employees
Manager salary: 70000
Adding new employee!
3. Java program to Implement hierarchical inheritance
public class HierarchicalInheritance
{
         public static void main(String[] args)
         {
                  Car car = new Car();     // Create objects of child classes
                  Motorcycle motorcycle = new Motorcycle();
                   car.display(); // Calling methods of parent class
                   motorcycle.display();
                   car.displayCar();     // Calling methods of child classes
                   motorcycle.displayMotorcycle();
        }
}
class Vehicle // Parent class
{
        void display()
        {
                System.out.println("This is a Vehicle");
        }
}
class Car extends Vehicle      // Child class 1
{
        void displayCar()
        {
                System.out.println("This is a Car");
        }
                                                                                3
}
class Motorcycle extends Vehicle        // Child class 2
{
        void displayMotorcycle()
        {
                System.out.println("This is a Motorcycle");
        }
}
Output
This is a Vehicle
This is a Vehicle
This is a Car
This is a Motorcycle
3. Java Program to illustrate Open Closed Principle
// class 1
// To store dimensions of a cuboid used to store length, breadth and height of a cuboid
class Cuboid
{
        public double length;
        public double breadth;
        public double height;
}
// Class 2
//To store dimensions of a sphere
class Sphere
{
        // Storing radius of a sphere
        public double radius;
}
// Class 3
// This class helps to calculate the volume of geometric objects
class Application {
        // Returning the total volume of the geometric objects
        public double get_total_volume(Cuboid[] c_geo_objects,Sphere[] s_geo_objects)
        {
                double vol_sum = 0;
                  // Iteratively calculating the volume of each Cuboid and adding it to the total volume
                  // Iterating using for each loop to calculate the volume of a cuboid
                  for (Cuboid geo_obj : c_geo_objects)
              {
                          vol_sum += geo_obj.length * geo_obj.breadth
                                        * geo_obj.height;
                  }
                                                                                                           4
                 // Iterating using for each loop to calculate the volume of a cuboid
                 for (Sphere geo_obj : s_geo_objects)
                {
                          // Iteratively calculating the volume of each
                          // Sphere and adding it to the total volume
                          vol_sum += (4 / 3) * Math.PI * geo_obj.radius
                                            * geo_obj.radius * geo_obj.radius;
                 }
                 // Returning the to total volume
                 return vol_sum;
        }
}
// Class 4 Main class
public class GFG {
         public static void main(String args[])
         {
                  // Initializing a cuboid one as well as declaring
                  // its dimensions.
                  Cuboid cb1 = new Cuboid();
                  cb1.length = 5;
                  cb1.breadth = 10;
                  cb1.height = 15;
                 // Initializing a cuboid two as well as declaring its dimensions.
                 Cuboid cb2 = new Cuboid();
                 cb2.length = 2;
                 cb2.breadth = 4;
                 cb2.height = 6;
                 ////Initializing a cuboid three as well as declaring its dimensions.
                 Cuboid cb3 = new Cuboid();
                 cb3.length = 3;
                 cb3.breadth = 12;
                 cb3.height = 15;
                 // Initializing and declaring an array of cuboids
                 Cuboid[] c_arr = new Cuboid[3];
                 c_arr[0] = cb1;
                 c_arr[1] = cb2;
                 c_arr[2] = cb3;
                 // Initializing a sphere one as well as declaring its dimension.
                 Sphere sp1 = new Sphere();
                 sp1.radius = 5;
                 // Initializing a sphere two as well as declaring its dimension.
                 Sphere sp2 = new Sphere();
                 sp2.radius = 2;
                 Sphere sp3 = new Sphere();
                                                                                        5
        sp3.radius = 3;
        // Initializing and declaring an array of spheres
        Sphere[] s_arr = new Sphere[3];
        s_arr[0] = sp1;
        s_arr[1] = sp2;
        s_arr[2] = sp3;
        // Initializing Application class
        Application app = new Application();
        double vol = app.get_total_volume(c_arr, s_arr);
        // Print and display the total volume
        System.out.println("The total volume is " + vol);
    }
}