0
\$\begingroup\$

I have a custom entity class and what it does is create a SQUARE object.

public class Entity {
    Rectangle rect;

public Entity() {
    this.rect = new Rectangle(20,20);
    rect.relocate(200,200);
}

// I want it to be public Entity getEntity() {return this.rect;}
public Rectangle getEntity() {  
    return this.rect;
  }       
}

The problem is that I can't get my class to return an Entity object, instead of a Rectangle one.

How do I go about doing that?

\$\endgroup\$
  • \$\begingroup\$ I've left an answer, but i'm not completely clear on why you want to return an Entity in the first place. If you update your question, perhaps we can help you better. \$\endgroup\$ – ThatOneGuy Feb 5 '14 at 11:37
1
\$\begingroup\$
// I want it to be public Entity getEntity() {return this.rect;}
public Rectangle getEntity() {  
    return this.rect;
}       

You have it all wrong, you seem to say you want to return this.rect and also have the function state that it returns an entity. That cannot be done. Te object you return has to be of the same type or a subclass type of the object your function definition/declaration says you"ll return.

For instance you can't do:

...
Orange orange
public Apple getApple() {  
    return this.orange;
}

You can do:

class MagicApple extends Apple{}
...
MagicApple magicApple
public Apple getApple() {  
    return this.magicApple;
}

You need to read more about oop, inheritance, polymorphism and strongly typed languages.

\$\endgroup\$
1
\$\begingroup\$

In your code,

public Rectangle getEntity() {  
    return this.rect;
}   

The "Rectangle" before getEntity() is the type of variable that the function will return. If you want the function to return another type, simply change it.

public Entity getEntity() {  
    return this.rect; 
}   

The function is now set to return an Entity type, but still returns a Rectangle type. This will give you an error(as you've probably noticed). So the return statement has to be adjusted as well.

public Entity getEntity() {  
    return new Entity(); 
}   

Now the function will return a new instance of the Entity Class. But i suspect you want to return a specific Entity.

private Entity specificEntity = new Entity();
public Entity getEntity() {  
    return specificEntity;
} 
\$\endgroup\$
0
\$\begingroup\$

If you are calling getEntity() then you already have the Entity object, it is what you are calling getEntity() on. If you want it to return another Entity object other than the one you already have you can make a method similar to this:

public Entity getEntity(){
    return new Entity();
}

although i suspect you want the Entity object that you already have.

If this isn't what you intended please clarify your question, it looks like you want to return Rectangle as an Entity which is not possible unless it is a subclass of Entity, maybe you can explain why you want to do this?

\$\endgroup\$
  • \$\begingroup\$ So I can create an uniform grid that runs collision on an arraylist of entity objects. My game will consist of rectangles, circles and squares. I can't have a single arraylist of all these at the same time, can I? \$\endgroup\$ – Gregg1989 Feb 5 '14 at 14:21
  • \$\begingroup\$ You can, if they all inherit from the same super type. Let's say the super type is Shape. Shape would have 3 subclasses: Rectangle, Circle and Square. Then you can create an ArrayList<Shape>. You should look into Inheritance and other OOP concepts. \$\endgroup\$ – Lucas Tulio Feb 5 '14 at 16:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.