public interface Shape { }
class Circle implements Shape {
Circle(int radius) { /* ... */ }
}
class Rectangle implements Shape {
Rectangle(int height, int width) { /* ... */ }
}
public class Main {
public static void main (String[] args) {
Shape[] shapes = new Shape[2];
shapes[0] = new Circle(1);
shapes[1] = new Rectangle(1, 2);
writeShapes(shapes);
}
public static void writeShapes(Shape[] shapes){
for(Shape shape:shapes){
if(shape instanceof Circle) System.out.println("Circle");
else if(shape instanceof Rectangle) System.out.println("Rectangle");
}
}
In the example, I want to add new shape. However, I could not get the point what's going on about the error "The public type Main must be defined in its own file" which occurs on my main method. I have tried to add "static" in front of main, but it does not work! Any suggestions are appreciated!