Note that there are some explanatory texts on larger screens.

plurals
  1. POJava why is implementing an interface necessary?
    text
    copied!<p>I've just started Java, and are going through examples online at: <a href="http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html</a></p> <p>Then I got to implementing an interface, while I understand the concept behind it, but it seems weird to me, as in the interface declaration you only defines it, and in the class that implements this interface you'll still have to write it's functions. So why use it at all?</p> <p>I tried the sample code, and then change the code to remove the interface, and they both work the same. So my question is when will implementing interface be used? It looks unnecessary to me. Thanks in advance!</p> <p>Sample Code online:</p> <pre><code>public class RectanglePlus implements Relatable { public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { origin = p; } public RectanglePlus(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public RectanglePlus(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing // the area of the rectangle public int getArea() { return width * height; } // a method required to implement // the Relatable interface public int isLargerThan(Relatable other) { RectanglePlus otherRect = (RectanglePlus)other; if (this.getArea() &lt; otherRect.getArea()) return -1; else if (this.getArea() &gt; otherRect.getArea()) return 1; else return 0; } } </code></pre> <p>Code that I changed to, taken out the interface, it still works the same</p> <pre><code>public class RectanglePlus { public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { origin = p; } public RectanglePlus(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public RectanglePlus(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing // the area of the rectangle public int getArea() { return width * height; } // a method required to implement // the Relatable interface public int isLargerThan(RectanglePlus otherRect) { if (this.getArea() &lt; otherRect.getArea()) return -1; else if (this.getArea() &gt; otherRect.getArea()) return 1; else return 0; } public static void main( String[] args ) { RectanglePlus newRect = new RectanglePlus(20, 30); RectanglePlus somerect = new RectanglePlus(50, 100); System.out.println("Area of newRect is " + newRect.getArea()); System.out.println("Area of somerect is " + somerect.getArea()); if((newRect.isLargerThan(somerect))==1) { System.out.println("newRect is bigger"); } else { System.out.println("somerect is bigger"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload