Error in getSize() getter - creating a size property that is an int array of height and width

Experts:

I'm trying to create a size property that is an int array of height and width:

      private int [] size = new int [2];

      public int [] getSize() {  // getSize() is underlined in red "here is my error"
            size[0] = height;
            size[1] = width;
            return size;
      }

      public void setSize(int h, int w) {
            height = h;
            width = w;
      }

When I hover the cursor over the getSize() it pops up the following message:  
The return type is incompatible with Component.getSize()

I've gone through all of my code looking for incompatibilities and I can't find any.  So, I'm beginning to think there is something wrong with my syntax.

Any ideas?
Thanks,
Patricia
pdlarueAsked:
Who is Participating?
 
ADSLMarkConnect With a Mentor Commented:
Explanation:

You are trying to override a method from a superclass with the name: "getSize". Since this method returns a Dimension, you cannot define your own method "getSize" with the same type of parameters but a different return type.
Imagine the situation where you have:

int[] getSize(int width, int height);

and

Dimension getSize(int width, int height);

Now if i were to call getSize(800,600); which method should i choose? Java is not smart enough to know this, so it doesnt allow you to have these two functions simultaneous. Simple solutions are returning the correct type or changing the method name a bit, like getMySize(..).

Good luck,
Mark
0
 
CEHJConnect With a Mentor Commented:
return a type of java.awt.Dimension
0
 
CEHJConnect With a Mentor Commented:
public Dimension getSize() {
      return new Dimension(width, height);
}
0
 
CPColinConnect With a Mentor Senior Java ArchitectCommented:
Or, change your getSize method name. It is conflicting with the getSize method in Component, and you can't have a method with the same name and parameters but a different return type.
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.