Solved

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

Posted on 2007-03-27
5
319 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:pdlarue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 18801764
return a type of java.awt.Dimension
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 18801783
public Dimension getSize() {
      return new Dimension(width, height);
}
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 20 total points
ID: 18801786
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
 
LVL 10

Accepted Solution

by:
ADSLMark earned 40 total points
ID: 18801820
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801964
:-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
jboss wildfly 10.1 10 607
How to fix  socket closed error 11 64
Session in java desktop 5 37
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question