Results 1 to 4 of 4
- 04-04-2011, 07:49 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 04-04-2011, 08:02 AM #2
- 04-04-2011, 08:08 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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?
- 04-04-2011, 08:25 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
The method above can now take any type of runnable like theseJava Code:public void myMethod(Runnable r){ //do stuff with r }
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.Java Code:public class x implements runnable{} public class y implements runnable{} public class z implements runnable{}
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
^^ IS ILLEGAL, you cannot have multi inheritance in javaJava Code:class y{} class x{} public class z extends y, x{}
However, this is legal, you can have multi implementations
^^ IS LEGAL.Java Code:interface x{} interface y{} public class z implements x, y{}
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
-
Tab & Alt Tab functionality in JTable
By nith21 in forum AWT / SwingReplies: 0Last Post: 02-01-2011, 11:50 AM -
Interface Question
By superman1938 in forum Advanced JavaReplies: 1Last Post: 12-14-2010, 04:28 AM -
Java Native Interface Question
By RichWade in forum Advanced JavaReplies: 20Last Post: 11-29-2010, 06:26 PM -
question on listener interface
By Minu in forum Java ServletReplies: 1Last Post: 01-16-2009, 10:33 AM -
Interface question in java
By tony404 in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 11:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks