Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy should I use interface in this situation in Java?
    primarykey
    data
    text
    <p>I'm trying to understand the basics of Java OOP concepts so I've a question about the interfaces as it confuses me a little. Below I was playing around with two classes. One which implements the <em>SizeComparable</em> interface and the other which doesn't but works too. </p> <pre><code>public interface SizeComparable { int isHigher(SizeComparable obj); } public class Interesting implements SizeComparable { private int height; public Interesting(int height) { this.height = height; } public int getHeight() { return height; } public int isHigher(SizeComparable obj) { Interesting otherInteresting = (Interesting)obj; if(this.getHeight() &gt; otherInteresting.getHeight()) { return 1; } else { return 0; } } public static void main(String[] args) { Interesting i1 = new Interesting(182); Interesting i2 = new Interesting(69); int result = i1.isHigher(i2); System.out.println("Is i1 higher than i2? Result: " + result); } } </code></pre> <p>How is the code above better than the code bellow? Personally I don't understand because the code bellow those it's job great too. Am I missing some concepts behind the interface idea?</p> <pre><code>public class Interesting { private int height; public Interesting(int height) { this.height = height; } public int getHeight() { return height; } public int isHigher(Interesting obj) { if(this.getHeight() &gt; obj.getHeight()) { return 1; } else { return 0; } } public static void main(String[] args) { Interesting i1 = new Interesting(182); Interesting i2 = new Interesting(69); int result = i1.isHigher(i2); System.out.println("Is i1 higher than i2? Result: " + result); } } </code></pre> <p>I was trying to understand it (<a href="http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html" rel="nofollow" title="here">here</a>), but I'm still unsure about this. Sorry if the question is a little silly, i just want to understand it completely.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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