Results 1 to 4 of 4
  1. #1
    Morning-owl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Question concerning interface functionality

    I am quite new to Java, and I wonder how it is possible for an interface's methods to have empty bodies yet still be able to complete different tasks when invoked by classes. How can something undefined be functional?

    The Runnable-interface, for example, only has one method, which has an empty body. How does that method receive its functionality, if it is not defined in the Runnable-interface? Does it receive the functionality from a class? In such a case, how do I assign functionality to an interface's methods? Thanks by advance.;)
    Best regards,
    Morning-owl.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Morning-owl View Post
    I wonder how it is possible for an interface's methods to have empty bodies yet still be able to complete different tasks when invoked by classes.
    It is because the functionality is provided by the classes that implement the Interface. Any class that implements an Interface MUST provide concrete methods for all the abstract methods defined in the Interface. Otherwise you get a compiler error.

  3. #3
    Morning-owl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Does that mean that the Thread-class gives the functionality to the run() method found in the Runnable interface? How does that work anyway? I mean, what is the point of having interfaces if I am going to provide concrete methods for all of its abstract methods?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you create a thread you supply it with a runnable. Through polymorphism you can make a method that takes a type of interface and then it can be given any class that implements that interface.

    Java Code:
    public void myMethod(Runnable r){
      //do stuff with r
    }
    The method above can now take any type of runnable like these

    Java Code:
    public class x implements runnable{}
    public class y implements runnable{}
    public class z implements runnable{}
    With an interface you can define a common set of methods that multiple classes use in different ways and the compiler can make sure each class is implementing all the methods.

    Check out the following link for more on interfaces.
    What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    Another reason interfaces are important in java is because they essentially allow multi implementation. While the following is illegal because a class can only subclass one other class
    Java Code:
    class y{}
    class x{}
    public class z extends y, x{}
    ^^ IS ILLEGAL, you cannot have multi inheritance in java

    However, this is legal, you can have multi implementations
    Java Code:
    interface x{}
    interface y{}
    public class z implements x, y{}
    ^^ IS LEGAL.

    If you are wanting to find more good reasons why interfaces are useful, try using google.

    Google the term "Why interfaces are inportant in java"

    Here are a few I found that you can read
    Interface in Java
    javajunkies.org (937) Java Basics: Interfaces (Tutorial)
    Last edited by sunde887; 04-04-2011 at 08:27 PM.

Similar Threads

  1. Tab & Alt Tab functionality in JTable
    By nith21 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 11:50 AM
  2. Interface Question
    By superman1938 in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2010, 04:28 AM
  3. Java Native Interface Question
    By RichWade in forum Advanced Java
    Replies: 20
    Last Post: 11-29-2010, 06:26 PM
  4. question on listener interface
    By Minu in forum Java Servlet
    Replies: 1
    Last Post: 01-16-2009, 10:33 AM
  5. Interface question in java
    By tony404 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 11:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •