0

I have a question regarding interfaces. I have a interface that has all the methods of a class called GameWorld. The GameWorld holds a class called GameObject that can be tanks, rocks, missiles, etc.

public interface IGameWorld
{
public Point getLocation(); // GameObject
public Color getColor();
public void setLocation(Point newCoord);
public void setColor(Color newColor);
}

I want to implement the methods in IGameWorld in GameWorld's GameObject class. Something like this.

public class GameWorld implements IGameWorld
{
 // vector to hold gameobjects
public class GameObject 
{
 private Point location;
 private Color color; // all objects have a random color 
GameObject()
{
 // method 
}
public Point getLocation()
{
 // method
}
public Color getColor()
{
 // method
}
public void setLocation (Point newCoord)
{
 // method
}
public void setColor (Color newColor)
{
 // method
}
 }
}

Is that possible? And if not, what is the alternative to doing this? Thanks.

asked Oct 30, 2013 at 6:31
1
  • 1
    That is what interfaces are for isn't it? Commented Oct 30, 2013 at 6:34

3 Answers 3

1

You make instances/objects of classes. No need to define a separate class which represent your object. Anyway what you have done is wrong because class GameWorld implements IGameWorld and GameWorld must implement all the methods and not GameObject class.

You can remove GameObject class

public class GameWorld implements IGameWorld
{
 private Point location;
 private Color color; // all objects have a random color 
public Point getLocation()
{
 // method
}
public Color getColor()
{
 // method
}
public void setLocation (Point newCoord)
{
 // method
}
public void setColor (Color newColor)
{
 // method
}
}

and then make instances/objects of this concrete class

IGameWorld gameObject = new GameWorld();
answered Oct 30, 2013 at 6:36
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll get back to you in a bit to see if it works out.
0

To above answer I would like to add that using interfaces makes our code as loosely coupled. So instead of using GameObject use interface where ever you need.

And your interface defines the common methods which are required for all the classes to create game and hence we can use as:

 IGameWorld marioGame = new GameWorld();
 IGameWorld tankGame = new GameWorld();

etc.

answered Oct 30, 2013 at 6:42

Comments

0

I don't think your GameObject class should be inside your GameWorld class. Try making them public classes in separate files. Then, only GameWorld will implement IGameWorld and your other classes (you mentioned tanks, rocks, missiles, etc.) will extend GameObject.

answered Oct 30, 2013 at 7:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.