In learning the Java language, after having been almost thoroughly confused by the talk on Lambda expressions, I have arrived at the Interfaces and Inheritance lesson.
My question is, what on Earth is the point of an interface?
What I have seen so far is stuff like this:
interface Interface { void doSomething(int x); } public void doSomething(int x) { //Some code }
In other words, you have to type out the method declaration yet again when you go to use this method. That code would still run without that interface declared!
The website gave some hypothetical story about software engineers from related fields needing to use the same methods, and so the interface is like a "contract." But if that's so, why not just sent around a memo to everybody who is working on the same technology with a list of the method names they are to use?
One more example to make my point: They gave an example of a method that compares rectangles (given in an interface), and then said "If you make a point of implementing Relatable in a wide variety of classes, the objects instantiated from any of those classes can be compared with the findLargest() method—provided that both objects are of the same class."
Well, I can do that without their interface:
class Triangle { //Code to create a Triangle object. public static int compareSize(Triangle t1, Triangle t2) { if(t1.getArea() > t2.getArea()) return 1; else if(t1.getArea() < t2.getArea()) return -1; else return 0; } }
The static method in that code can always be called and fed any two triangle objects and will return a comparison result. No interface needed.
Here is the page where they talk about interfaces if anyone is interested: Implementing an Interface (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
The only possible use I've seen for interfaces is with Lambda expressions, and my head is still spinning from that "lesson" so I probably wouldn't know how to properly use one of those even if I wanted to.
Thanks,
-summit45