Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

Interfaces

Interfaces are used to handle events for the most part. An interface defines a protocol of behavior. This means that the class implementing the interface must support certain required methods. There are two ways to implement the interface:

  • Use the "implements" keyword. The implementing class must provide an implementation for all the interface object's methods.
    import java.awt.event.*;
    public class MyClass implements Interfacename
    {
      //Attributes and methods go here
    }
    
  • Use an inner class (nonstatic nested class). The interface objects methods are already provided and can be optionally overridden.

An interface defines methods but does not implement them The keyword "interface" prevents function definitions in the interface. The class that implements the interface must implement the methods of the interface. The interface is written as follows:

public interface MyInterface
{
   public void Method1();
   public void Method2();

   public int usefuldata1;
}

All methods in the interface are public and abstract. The data or objects defined in the interface are inherited by classes implementing the interface and those implementing classes must define the methods. A class can only inherit constants from an interface and cannot inherit implemented methods. An interface can have a superinterface if declared with a line like:

public interface MyInterface extends MySuperInterface

The interface can extend to several interfaces by using a comma delimited list.