Post

Created by @nathanedwards
 at October 31st 2023, 7:27:56 am.

Question:

Write a Java program that demonstrates inheritance and polymorphism. Create a base class called "Shape" with the following attributes and methods:

Attributes:

  • name - representing the name of the shape
  • area - representing the area of the shape

Methods:

  • getName() - returns the name of the shape
  • setName(name: String) - sets the name of the shape
  • calculateArea() - calculates and sets the area of the shape

Next, create two derived classes from the Shape class:

  1. Rectangle Class:
  • This class should inherit the attributes and methods from the Shape class.
  • Implement a constructor that accepts the length and width of the rectangle as parameters and sets them appropriately.
  • Implement the calculateArea() method to calculate and set the area of the rectangle.
  1. Circle Class:
  • This class should also inherit the attributes and methods from the Shape class.
  • Implement a constructor that accepts the radius of the circle as a parameter and sets it appropriately.
  • Implement the calculateArea() method to calculate and set the area of the circle.

Lastly, write a main method in a separate class to demonstrate the usage of these classes. Create objects of both Rectangle and Circle classes, set their names, and calculate their areas. Print out the details of each shape.

Your program should produce the following output:

Rectangle:
Name: Rectangle 1
Area: 24.0

Circle:
Name: Circle 1
Area: 78.54

Answer:

The complete Java program to demonstrate inheritance and polymorphism is as follows:

// Shape class
class Shape {
    protected String name;
    protected double area;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getArea() {
        return area;
    }

    public void calculateArea() {
        // Overridden by derived classes
    }
}

// Rectangle class
class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public void calculateArea() {
        area = length * width;
    }
}

// Circle class
class Circle extends Shape {
    private double radius;

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

    @Override
    public void calculateArea() {
        area = Math.PI * Math.pow(radius, 2);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Creating objects of Rectangle and Circle
        Rectangle rectangle = new Rectangle(4.0, 6.0);
        Circle circle = new Circle(5.0);

        // Setting names of the shapes
        rectangle.setName("Rectangle 1");
        circle.setName("Circle 1");

        // Calculating areas
        rectangle.calculateArea();
        circle.calculateArea();

        // Printing details of each shape
        System.out.println("Rectangle:\nName: " + rectangle.getName() + "\nArea: " + rectangle.getArea() + "\n");
        System.out.println("Circle:\nName: " + circle.getName() + "\nArea: " + circle.getArea());
    }
}

Explanation:

  • The program starts by defining a base class called Shape with attributes name and area and three methods - getName(), setName(name: String), and calculateArea().
  • getName() and setName(name: String) are simple getter and setter methods for the name attribute respectively.
  • The calculateArea() method is declared as abstract in the Shape class since it will be overridden by derived classes.
  • Next, two derived classes Rectangle and Circle are defined, both inheriting from the Shape class.
  • The Rectangle class has additional attributes length and width, and overrides the calculateArea() method to calculate the area of a rectangle.
  • The Circle class has an additional attribute radius, and overrides the calculateArea() method to calculate the area of a circle.
  • In the main method, objects of Rectangle and Circle classes are created.
  • The names of the shapes are set using the setName() method.
  • The calculateArea() method is called on each shape object to calculate and set their respective areas.
  • Finally, the details of each shape object (name and area) are printed using the getName() and getArea() methods.

When you run the program, it will output:

Rectangle:
Name: Rectangle 1
Area: 24.0

Circle:
Name: Circle 1
Area: 78.54

This demonstrates the concept of inheritance and polymorphism, as well as the usage of objects and overridden methods in Java.