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 shapearea
- representing the area of the shapeMethods:
getName()
- returns the name of the shapesetName(name: String)
- sets the name of the shapecalculateArea()
- calculates and sets the area of the shapeNext, create two derived classes from the Shape class:
calculateArea()
method to calculate and set the area of the rectangle.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:
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.calculateArea()
method is declared as abstract in the Shape
class since it will be overridden by derived classes.Rectangle
and Circle
are defined, both inheriting from the Shape
class.Rectangle
class has additional attributes length
and width
, and overrides the calculateArea()
method to calculate the area of a rectangle.Circle
class has an additional attribute radius
, and overrides the calculateArea()
method to calculate the area of a circle.main
method, objects of Rectangle
and Circle
classes are created.setName()
method.calculateArea()
method is called on each shape object to calculate and set their respective areas.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.