Here is the code:
public static void main(String args[])
{
Circle objCircle = new Circle(5);
Square objSquare = new Square(5);
Triangle objTriangle = new Triangle(3, 4);
Sphere objSphere = new Sphere(5);
Cube objCube = new Cube(5);
Tetrahedron objTetra = new Tetrahedron(5);
Object[] Shape = new Object[5];
Shape[0] = objCircle;
Shape[1] = objSquare;
Shape[2] = objTriangle;
Shape[3] = objSphere;
Shape[4] = objCube;
Shape[5] = objTetra;
for(int i = 0; i<Shape.length; i++)
{
//Problem occured in this section of code
System.out.println(Shape[i].getArea());
}
}
}
I have six different classes and all of them have getArray() method with definition of their own. I want to print the values that getArray() method returns from different classes using the Shape array.
getArea()method? If so, just change your array to be an array of that type rather thanObject[].ArrayIndexOutOfBoundsException. Do not useObject[]. UseShape[]orList<Shape>whereShapeis an interface with agetAreamethod.