just Learned Interfaces

thay are really good to know if do alot of things like items/animals/AI

i have always created 100 moveMethods for animals like

public void moveCow(){
}
public void movePig(){
}
public void moveZombie(){
}

now thay all have just one called

public void move(){
}
so mutch easyier

Yeah…

Humm…you don’t need an interface for this just method overloading. And really you don’t even need that.

what xD?

You could use a structure where you have a superclass and a child class that extends that superclass and inherits the parent functions. An interface here isn’t the best choice.

… and if you want to be really obscure you can do all method calls “by name” through reflection.

How was my answer obscure?

Interfaces are particularly useful if your code involves a common type which has many implementations. The best example here would be the Java Collections API. There is an interface called [icode]List[/icode] and there are a lot of implementations of it like [icode]ArrayList[/icode], [icode]LinkedList[/icode] etc.,

As @opiop65 pointed out, interfaces are not the best option here. You could instead go with a superclass with subclasses since you are only seeking method overloading.

It’s not been about your answer. It was a (inserious) suggestion to the original poster. Smalltalk programmers won’t think it’s obscure though, since in smalltalk you just need the method name and no interface or super class.

Ah, I’m sorry! Thought that was directed at me and I was confused.

it works greate so far lols

So you’re going to ignore everyone that posted here who all have more experience than you? That’s wise, let me know how that turns out.

am not recoding this

public class Player implements Entity{

	int w = 64;
	int h = 64;
	int damage = 7;
	int health = 100;
	int lastHealth = health - 1;
	boolean isDead = false;
	boolean isDamaged;
	static boolean isMoving = false;
	
	boolean MovingRight = false;
	boolean MovingLeft= false;
	boolean MovingUp= false;
	boolean MovingDown= false;
	
	public static int MouseDetection = 150;
	
	public  String Username = "";
	public static Vector2F pos = new Vector2F(0,0);
	public static Vector2F moveto = new Vector2F(0,0);
	
	public Player(int x, int y) {
		this.pos.x = (int) x;
		this.pos.y = (int) y;
	}
	
	BufferedImage image;
	
	@Override
	public void SetEntityImage(BufferedImage image) {
		this.image = image;
	}
	
	@Override
	public void SetEntityImageWidthAndHeight(int w, int h) {
		this.w = w;
		this.h = h;
	}
	
	@Override
	public void RenderEntity(Graphics2D g2d) {
		
		if(image != null){
			//g2d.drawImage(image,  (int) pos.getWorldLocation().x, (int) pos.getWorldLocation().y, w , h, null);
			//g2d.drawRect((int)pos.getWorldLocation().x, (int)pos.getWorldLocation().x, w, h);
			//g2d.drawRect((int)pos.x, (int)pos.x, w, h);
			
			g2d.drawImage(image, (int)Player.pos.getWorldLocation().x,(int) Player.pos.getWorldLocation().y,  w , h , null);
			g2d.drawRect((int)Player.moveto.getWorldLocation().x - 64 + 32 , (int)Player.moveto.getWorldLocation().y - 64 + 32, 128, 128);
			g2d.drawRect((int)Player.pos.getWorldLocation().x ,(int) Player.pos.getWorldLocation().y, w, h);
			Sys.sendSytemMessage(isMoving +"Rendering");
			MoveToLocation();
		}

	}

	private void MoveToLocation() {
			if(Player.pos.getWorldLocation().x < Player.moveto.getWorldLocation().x)
			{
				if(Math.abs(Player.moveto.x - Player.pos.x) > 1)
				{
					Player.pos.x-=0.2;
					isMoving = true;
				}else{
					isMoving = false;
				}
			}
			else
			{
				if(Math.abs(Player.moveto.x - Player.pos.x) > 1)
				{
					Player.pos.x+=0.2;
					isMoving = true;
				}else{
					isMoving = false;
				}
			}
		if(Player.pos.getWorldLocation().y < Player.moveto.getWorldLocation().y)
		{
			if(Math.abs(Player.moveto.y - Player.pos.y) > 1)
			{
				Player.pos.y-=0.2;
				isMoving = true;
			}else{
				isMoving = false;
			}
		}
		else
		{
			if(Math.abs(Player.moveto.y - Player.pos.y) > 1)
			{
				Player.pos.y+=0.2;
				isMoving = true;
			}else{
				isMoving = false;
			}
		}
	}

	@Override
	public void move(float x,float y) {
		this.pos.x += (int) x;
		this.pos.y += (int) y;
	}

	@Override
	public void damage(int amount) {
		this.damage = amount;
	}

	@Override
	public void hurt(int amount) {
		health -= amount;
	}

	@Override
	public void isDead(boolean isDead) {
		this.isDead = isDead;
	}

	@Override
	public void isDamaged() {
		if(health < lastHealth){
			isDamaged = true;
		}else{
			isDamaged = false;
		}
	}

	@Override
	public void isColliding() {}

	@Override
	public void isMoving() {
		if(isMoving = true){

		}
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle((int)pos.x, (int)pos.y, w, h);
	}

	@Override
	public Rectangle getWorldBounds() {
		return new Rectangle((int)pos.getWorldLocation().x, (int)pos.getWorldLocation().y, w, h);
	}

	@Override
	public void moveWorldLocation(float x, float y) {
		this.pos.getWorldLocation().x += x;		
		this.pos.getWorldLocation().y += y;		
	}

	@Override
	public void mouseDragged(MouseEvent e) {}

	@Override
	public void mouseMoved(MouseEvent e) {
			int y = e.getY();
			int x = e.getX();
	}

	@Override
	public void mouseClicked(MouseEvent e) {}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {
		if (SwingUtilities.isRightMouseButton(e)) {
			Vector2F pos = new Vector2F(e.getX(),e.getY());
			Player.moveto.x = pos.getWorldLocation().x;
			Player.moveto.y = pos.getWorldLocation().y;
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {}


}

Thats actually not a lot of code. Plus, better to do it now than later.

Less than 200 lines, and looks like it would be half that when properly refactored.

Google is your friend. This is a very basic and very broad question. I’ll give you some basics about it though.

An interface is pretty much a partial template for the class. It forces a class that “implements” an interface to have all the functions that the interface states and forces you to define what they do.

If you know what an abstract/virtual method is. A interface is a class that is made up of only abstract/virtual methods.

for example, you mentioned the runnable interface. The whole interface simply looks like this.


public interface Runnable
{
    public void run();
}

so anything that implements Runnable must have the run() function, but every class defines the run function themselves. This guarantees that any Runnable object has the method run().

Ill give you a simple example.


public interface Drawable
{
    public void draw();
}

Example class that uses drawable:


public class Car implements Drawable
{
   //other car methods
     public void draw()
     {
           //explain how to draw a car
     }
}


public class Game
{
    Drawable[] drawThese;
    public Game()
    {
            drawThese = new Drawable[2];
           drawThese[0] = new Car(); //polymorphism
           drawThese[1] = new otherDrawableThing();
    }
    public void gameLoop()
    {
           //do other game loop stuff here
           for(int i = 0; i < drawThese.length; i++)
                 drawThese[i].draw();     //all drawable objects have this method tho they
                                                    //define them differently
    }
}

edit: there is a lot more to learn. Google. Also note the useful characteristic that you can implement as many interfaces as you want and you can only extend from one class in Java.

Interface is not a class. When you do:


public interface Runnable {
	public void run();
}

{
	Thread thread = new Thread(new Runnable(){
		public void run(){
			System.out.println("overriding runnable meethod..");
		}
	}).start();
}

new Runnable means that you’re creating a new class, which implements runnable.

The reason interfaces exist is for cross-cuts. Draw a picture of a tree. That represent a class hierarchy. Now if you need common functionality down the tree in nodes whose common parent are far toward the root then you need an interface to make them type compatible for that functionality. Or similarly to make type compatible classes that creating a common base class is impossible/impractical because they are out of your control.

Really, guys and gals, this is a question which is extremely simple to find many answers to. Here’s the top 3 google results for ‘java interface’:

  • What Is an Interface? (The Java™ Tutorials)
  • Interfaces (The Java™ Tutorials)
  • Interface (Java) on Wikipedia

Each of which are very clear and useable explanations of what an Interface in Java is.

I’d note that “design by interface” has been fashionable for some number of years and is a pretty poor model.