javaarrayspolymorphismcontainersheterogeneous

Managing different objects in an heterogeneous Java array


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:

It should be a nice object-oriented design solution.


Solution

  • Why don't you add an enum FigureType to your base class that identifies the child class?

    public static enum FigureType {
    
        Square,
        Circle
    }
    
    public static class Figure {
        private FigureType type;
    
        public Figure(FigureType type) {
            this.type = type;
        }
    
        public FigureType getType() {
            return type;
        }
    
        public void draw() {
        }
    
        public String getColor() {
            return null;
        }
    }
    

    You would have to add a default constructor to each child class that calls the parent class constructor with its FigureType.

    public static class Square extends Figure {
    
        public Square() {
            super(FigureType.Square);
        }
    
        @Override
        public void draw() {
            System.out.println("Square");
        }
    }
    
    public static class Circle extends Figure {
    
        public Circle() {
            super(FigureType.Circle);
        }
    
        @Override
        public void draw() {
            System.out.println("Circle");
        }
    
        public float getRadius() {
            return 8;
        }
    }
    

    Usage:

    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();
            if (figure.getType() == FigureType.Circle) {
                ((Circle) figure).getRadius();
            }
        }
    }
    

    Results:

    Circle
    Circle
    Square
    

    No exception