I have to solve this "container problem" in Java. I have an array made up with different figures and I'd like the following code to work:
package container;
class Figure{
public void draw() {}
public String getColor() { return null; }
}
class Square extends Figure{
@Override
public void draw(){
System.out.println("Square");
}
}
class Circle extends Figure{
@Override
public void draw(){
System.out.println("Circle");
}
public float getRadius(){
return 8;
}
}
public class Container {
public static void main(String[] args) {
Figure[] figures = new Figure[3];
figures[0]= new Circle();
figures[1]= new Circle();
figures[2]= new Square();
for(Figure figure:figures){
figure.getColor();
figure.draw();
((Circle) figure).getRadius();
}
}
}
Where you can see there is a problem because Square
hasn't got a getRadius()
method. I have the following restrictions:
- can't use generics
- can't use
instanceof
It should be a nice object-oriented design solution.