I am trying to understand Java interfaces like the millions of others around the world. How do I test that I’m really using my interface? If I remove the “implements” in my TestBubbles class I still get the same results. I can change either method definition and get a compilation failure but how do I test the data I’m passing?
public interface Bubbles {
public void addAir(String bubbleType, float bubbleOne, float bubbleTwo );
}
public class TestBubbles implements Bubbles {
public static void main(String [] args){
String type = "wiggly";
float sizeOne = 42.01f;
float sizeTwo = 80.10f;
TestBubbles tb = new TestBubbles();
tb.addAir(type, sizeOne, sizeTwo);
}
public void addAir(String rType, float fOne, float fTwo ){
System.out.println(rType + " " + fOne + " " + fTwo);
}
}
You test that, and you should code that by defining your variables with the type of the interface instead the type of your class.
Look at your line:
When programming to interfaces that should be coded as follows:
That way you later could exchange TestBubbles with SuperTrooperTestBubbles which implements the same Interface by only changing one line of code, and the rest will work:
Where