Interfaces

Interfaces

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

Interface is different from a class in several ways, including:

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Java does not allow you to create a subclass from two classes. There is no multiple inheritance. The major benefit of that is that all Java objects can have a common ancestor. That class is called Object. All Java classes can be up-casted to Object. Example:

Example Code section 4.16: Class declaration.
  1. class MyObject {
    
  2.  ...
    
  3. }
    

When you type the above code, it actually means the following:

Example Code section 4.17: Class extension.
  1. // The compiler adds 'extends Object'. if not specified
    
  2. class MyObject extends Object {
    
  3.  ...
    
  4. }
    

So, it can be guaranteed that all the Object class methods are available in all Java classes. This makes the language simpler.

To mimic multiple inheritance, Java offers interfaces, which are similar to abstract classes. In interfaces all methods are abstract by default, without the abstract keyword. Interfaces have no implementation and no variables, but constant values can be defined in interfaces. However, a single class can implement as many interfaces as required.

Example Code section 4.18: Interface declaration.
  1. public interface MyInterface {
    
  2.   public static final String CONSTANT = "This value can not be changed";
    
  3.  
    
  4.   public String methodOne(); // This method must be implemented by the class implementing this interface
    
  5.   ...
    
  6. }
    

...

Example Code section 4.19: Interface implementation.
  1. public class MyObject implements MyInterface {
    
  2.   // Implement MyInterface interface
    
  3.   public String methodOne() {
    
  4.    ...
    
  5.   }
    
  6. }
    

All constants are assumed to be final static and all methods public, even if the keywords are missing. When overriding methods defined in interfaces there are several rules to be followed:

  • Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
  • The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
  • An implementation class itself can be abstract and if so interface methods need not be implemented.

When implementing interfaces there are several rules:

  • A class can implement more than one interface at a time.
  • A class can extend only one class, but implement many interfaces.
  • An interface can extend another interface, similar to the way that a class can extend another class.
↑Jump back a section

Read in another language

This page is available in 1 language

Last modified on 8 April 2013, at 18:05