Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What good are interfaces?

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default What good are interfaces?

    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What good are interfaces?

    I wrote these articles quite a while ago. I'm not sure if they are even complete, and glancing through them I kind of want to re-write them.

    General CS Concepts: Abstraction
    General CS Concepts: Polymorphism
    General CS Concepts: Interfaces

    General broad over-view:

    Interfaces are only useful if we want to write handler code in a generic manner. It has nothing to do with lambdas (indeed, interfaces have been in Java since day 1, lambdas are a fairly new feature introduced in the latest version of Java). It allows us to declare that some class "has" some method implemented.

    Suppose we have the following classes TaskA and TaskB:

    public class TaskA implements Runnable
    {
        public void run()
        {
            System.out.println("Hello from TaskA!");
        }
    }

    public class TaskB implements Runnable
    {
        public void run()
        {
            System.out.println("Aloha from Task B!");
        }
    }

    They both implements the Runnable interface, which means the must contain the method public void run().

    We can use these classes directly and never have to deal with interfaces, polymorphism, or abstraction:

    public static void runner(TaskA var)
    {
        var.run();
    }
     
    public static void runner(TaskB var)
    {
        var.run();
    }

    But I just wrote the same code twice! It would get much worse if I decide later that I wanted to add classes TaskC, TaskD, ... etc. Why am I re-writing the exact same code multiple times just because I want to call the run method?

    It would be much simpler to have some type which explicitly states that the held variable has a run method. This is where Runnable comes in: that's exactly what it does.

    public static void runner(Runnable var)
    {
        var.run();
    }

    Polymorphism is only one such solution to this problem, and it works quite well. In Java this is the preferred solution because it works well, the syntax is clean, and conceptually is fairly easy to grasp.

    An alternative solution with Java is using reflection, however I would strongly caution against going down that path because reflection is a run-time tool, not a compile-time tool. The syntax isn't clean, and reflection will perform worse than polymorphism because it has extra work to do at runtime.

    In C++ there are templates (basically a suped-up much more powerful version of Java's generics) which could allow us to write similarly generic version of runner without ever having to touch interfaces or polymorphism. That being said, there are problems which templates or generics can't solve that Polymorphism can, and vise-versa.

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    jps (August 18th, 2013), summit45 (August 18th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What good are interfaces?

    Wow. I don't believe it. A concise, simple and useful answer about interfaces. After a quick Google search before posting this question, I didn't think such a thing existed, haha!

    Thank you very much. I get it now. I just wish this thread could be moved to the "front of the internet" so that others Googling the same question will find it. That post should be integrated into the Java tutorial on interfaces.

    Thanks again!

    -summit45

Similar Threads

  1. Today is a very good day and I'm in a good mood
    By AHefphern in forum The Cafe
    Replies: 0
    Last Post: May 15th, 2013, 04:10 AM
  2. Question Regarding Interfaces
    By davidrobin in forum Object Oriented Programming
    Replies: 1
    Last Post: April 18th, 2013, 05:10 PM
  3. Interfaces
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 13
    Last Post: December 7th, 2011, 07:58 PM
  4. marker interfaces
    By anupam0021 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:53 PM
  5. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 09:51 PM