12

I'm trying to make an abstract shape class for rectangle and ellipse the only abstract method I gave shape was the draw method, but after I gave it a constructor and everything it gives me an error in the rectangle class saying that color and the other variables have private accesses here is my code:

public abstract class Shape{
        private int x, y, width, height;
        private Color color;
        public Shape(int x, int y, int width, int height, Color color){
            setXY(x, y);
            setSize(width, height);
            setColor(color);
        }

        public boolean setXY(int x, int y){
            this.x=x;
            this.y=y;
            return true;
        }

        public boolean setSize(int width, int height){
            this.width=width;
            this.height=height;
            return true;
        }

        public boolean setColor(Color color){
            if(color==null)
                return false;
            this.color=color;
            return true;
        }

        public abstract void draw(Graphics g);
    }

    class Rectangle extends Shape{
        public Rectangle(int x, int y, int width, int height, Color color){
            super(x, y, width, height, color);
        }

        public void draw(Graphics g){
            setColor(color);
            fillRect(x, y, width, height);
            setColor(Color.BLACK);
            drawRect(x, y, width, height);
        }
    }

    class Ellipse extends Shape{
        public Ellipse(int x, int y, int width, int height, Color color){
            super(x, y, width, height, color);
        }

        public void draw(Graphics g){
            g.setColor(color);
            g.fillOval(x, y, width, height);
            g.setColor(Color.BLACK);
            g.drawOval(x, y, width, height);
        }
    }
starball
  • 20,030
  • 7
  • 43
  • 238
user3602628
  • 151
  • 1
  • 2
  • 6

2 Answers2

3

private int x, y, width, height; means that they can only be accessed from the actual class where they are declared. You should create appropriate get and set methods and use them. You want the fields to be either public or protected to access them using the dot notation, but IMO it's a better design to keep them private and use get and set. See also In Java, difference between default, public, protected, and private that explains the fields' visibility.

Community
  • 1
  • 1
Totò
  • 1,824
  • 15
  • 20
  • 1
    I'm sorry but I have trouble understanding how to use set and get methods could you provide a visual example? – user3602628 May 05 '14 at 01:19
  • in the superclass add the accessor methods for all the private fields e.g. `public int getX(){return this.x;}` etc... and then in the sub-classes (e.g. `Ellipse`) use them like: `g.fillOval(getX(), getY(), getWidth(), getHeight());` – Totò May 05 '14 at 01:21
  • So if I understand you correctly you're saying I need to add get methods to my super class (because I'm pretty sure I have setters) and set them to return true and then just put them in the the subclass replacing all the variables in parenthesis? – user3602628 May 05 '14 at 01:39
  • You need to add get methods in the super class, and set them to return what they have to return (e.g. `public int getWidth(){return this.width;}public int getX(){return this.x;}` ) and use them to retrieve the field value instead of accessing the field directly – Totò May 05 '14 at 01:42
  • Okay I did what you said and added get methods to all the setters and had them return true and I put the get methods inside the subclass just like in your example but now it's saying method getColor cannot be applied to given types. So what happened? – user3602628 May 05 '14 at 02:13
  • Sorry, I don't follow you... Why do the accessors method have to return `true`? The `setSomething` doesn't need to return anything, the `getSomeField` has to return `someField`. If you can update the code in your question with the latest changes we may be able to help. – Totò May 05 '14 at 02:33
1
g.setColor(color);
g.fillOval(x, y, width, height);

All these fields are private to the parent class.

You cannot access a private field from outside the class, not even from subclasses.

You could change the field to protected or provide getters for the subclasses to call.

So in Shape, add a method

protected Color getColor(){  return color; }

and then you can do

g.setColor(getColor());

in your subclasses. Same for the other fields.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Private variables cannot be accessed even in sub classes. You can either change that variable to protected (so it can accessed in sub classes and the classes in the same package) or use get color in the sub class also. – Thinesh Ganeshalingam May 05 '14 at 01:14