Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Erwin Paolo Yumul
13,852 PointsInterfaces in Java
I really don't understand interfaces and when to use one. Can you explain it to me more simple and clear. Thanks.
2 Answers
Brendon Butler
4,254 PointsLet's say you have a game with different types of Entities.
You have an Entity interface that is used as a sort of foundation for each entity (obviously).
public interface Entity {
public String getName();
public int getHealth();
}
Now lets say you want to have a Zombie class as well as a Skeleton class. You would implement the Entity class, and take in all the methods from it.
public class Zombie implements Entity {
private String name = "Zombie";
private int health = 16;
public String getName() {
return this.name;
}
public int getHealth() {
return this.health;
}
}
public class Skeleton implements Entity {
private String name = "Skeleton";
private int health = 12;
public String getName() {
return this.name;
}
public int getHealth() {
return this.health;
}
}
That's all well and good, but if we have two classes that are identical other than the variables within them, why do we need an interface? Good question -- you might not, it depends on how you plan on using these classes.
Let's say that you don't actually know whether or not the entity will be a skeleton or a zombie. You could use the Entity interface inside of methods to take in any kind of Entity.
// this method will take in any class that implemented the Entity interface
public void doSomething(Entity entity) {
if (entity instanceof Zombie) {
System.out.println("Entity is a zombie!");
Zombie zombie = (Zombie) entity; // easy casting Zombie to the entity variable
// casting essentially just changes the variable's type
}
if (entity instanceof Skeleton) {
System.out.println("Entity is a skeleton!");
Skeleton skeleton = (Skeleton) entity; // easy casting Skeleton to the entity variable
}
/*
* Let's say that the entity is a zombie. Since it's actual class is of the Zombie type, you would be
* able to access any of the Zombie classes methods using the entity interface
*/
entity.getName(); // this would return "Zombie" since it was defined in the Zombie class.
}
// example of the doSomething() method && creating a new instance of each class
Entity entity = new Zombie();
doSomething(entity);
doSomething(new Skeleton());
What if the entity class had a method that was implemented differently in both the Skeleton and Zombie class?
// for example, the skeleton's getHealth() method is implemented like this
public void getHealth() {
Random random = new Random();
return random.nextInt(); // returns a random integer value
}
// and this is how the zombie's getHealth() method is implemented
public void getHealth() {
return this.health;
}
/*
* There are three variable a Zombie variable, Skeleton Variable, and Entity variable that we don't know
* which type it is.
*/
skeleton.getHealth(); // would return a random integer value
zombie.getHealth(); // would return 16 since we defined it in the class
entity.getHealth(); // would either return 16 or a random integer value depending on how the "entity" variable was initialized
Jonathan Grieve
Treehouse Moderator 91,254 PointsI've spent a lot of time looking into this. I'm not sure I can tell you precisely what they are. It's more like a framework that you can use, built on classes.
We're taught to think of interfaces as like a "contract" that you sign a a programmer and then you do things in your script that deliver on that contract. You do this by running on default methods that java provides.
For example if you "implement" a Comparable interface on a class you'd use a compareTo() method.
public class NameOfClass implements Comparable {
}
This should point you in the right directio of what's out there in terms of interfaces. http://docs.oracle.com/javase/8/docs/api/