I was recently introduced to interfaces in Java, but I'm having trouble understanding their purpose. So, let's say I have the following:
//this is an interface
public interface DanceMoves {
public void theJitterbug(int twists);
public void theHustle(int steps);
public void theMoonwalk(int slides);
}
And then I have a class that implements this interface:
public class DanceClub implements DanceMoves {
public void theJitterbug(int twists) {
System.out.println("Twist " + twists + " times!");
}
public void theHustle(int steps) {
System.out.println("Step " + steps + " times!");
}
public void theMoonwalk(int slides) {
System.out.println("Slide " + slides + " times!");
}
//more methods can go here
public static void main(String[] args) {
DanceClub letsDance = new DanceClub();
letsDance.theJitterbug(5);
letsDance.theHustle(4);
letsDance.theMoonwalk(3);
}
}
And perhaps I have another method that implements this interface:
public class Diner implements DanceMoves {
public void theJitterbug(int twists) {
System.out.println("Twist " + twists + " times!");
System.out.println("We only do the jitterbug in diners!");
}
public void theHustle(int steps) {
}
public void theMoonwalk(int slides) {
}
public static void main(String[] args) {
Diner letsDanceAgain = new Diner();
letsDanceAgain.theJitterbug(5);
}
}
So my question is, why do we bother implementing an interface if we have to rewrite all of the interface methods anyways? How is this reusing code?
EDIT: Thanks a bunch to all who answered! I am new to Java, and the only other language I've learned so far is Jython. So while a lot of the concepts translate over quite nicely, some of the newer things are a bit harder for me to grasp.
I also appreciated the response example given below, as well as those links to previous questions and outside sources. I had actually read through most of those questions earlier and had been to those outside sources, but they were more helpful after I was able to develop a clearer understanding of what I was actually reading!
But anyways! What I really wanted to do was write down my big takeaway (for both my sake and the sake of posterity) about interfaces, and see if I really do "get it". My previous questions were, why do we bother implementing an interface if we have to rewrite all of the interface methods anyways? How is this reusing code?. Using Elliot Frisch's revision of my example:
public interface Dance { //an interface
public void boogie(int count);
}
Now, you could have methods, related or not, that implement this interface -
public class theMoonwalk implements Dance {
public void boogie(int count) {
System.out.println("Slide " + count + " times!");
}
public void mJ() {
System.out.println("Michael Jackson did this dance!");
}
public class theHustle implements Dance {
public void boogie(int steps) {
System.out.println("Step " + steps + " times!");
}
}
public class theJitterBug implements Dance {
public void boogie(int twists) {
System.out.println("Twist " + twists + " times!");
}
}
public class crazyUncle implements Dance {
public void boogie(int shimmy) {
System.out.println("I really don't know how to dance.");
}
public void yap() {
System.out.println("When I was your age...!");
}
}
Then to use it,
public static void main(String[] args) {
Dance[] dances = new Dance[] {
new theHustle(), new theMoonwalk(), new theJitterBug(), new crazyUncle()
};
for (Dance d : dances) { //for each element in this array of type Dance...
// Note: The caller just boogies. The object determines the "move".
d.boogie(3);
}
}
Now, here is my explanation for why I should bother implementing an interface if I have to rewrite all of the interface methods anyways:
Because the theHustle, theMoonwalk, theJitterbug and crazyUncle classes each implement the Dance interface, they've, in effect, "signed" a "contract" that guarantees that instances of these classes will, at minimum, contain the methods from the Dance interface. Thus, even though, for example, the theMoonwalk class is different from the crazyUncle class, theMoonwalk objects and crazyUncle objects can, for example, be included in an array of type Dance, because those objects contain the "DNA" of the Dance interface.
Further, I could have another method in a different class that takes in a parameter of type Dance:
public void danceExercise(Dance d, int num) {
d.boogie(num);
}
When calling the danceExercise method, for the Dance d parameter, I can provide any value that is of a type that implements the Dance interface. So, since crazyUncle and theMoonwalk both implement Dance, I can provide an instance of crazyUncle or an instance of theMoonwalk where a Dance is expected.
TL;DR Thanks all! I now have a sharper understanding of interfaces.