What is an Interface?
Home ] Up ] What's The Problem? ] The Solution ] Why Is That Better? ] Multiple Inheritance ] [ What is an Interface? ] Nouns vs. Adjectives ] Interface Inheritance ] Marker Interfaces ]

 

 

So, What is an Interface?

An interface has the following properties:

  • It is a type, just as a class is a type.
  • It may contain:
    • abstract method declarations (no implementations).  
      • Such methods are always (implicitly) public.
    • field (data) declarations that are implicitly public, static and final
      • synchronized, transient and volatile are not allowed
  • It may not be instantiated

Basically, and interface is similar to an abstract class that contains:

  • no method implementations, and
  • no instance fields

Interfaces allow you to "add in" multiple behaviors for a class. If you want to instantiate such a class, that class must implement all the abstract methods from all the interfaces it has said it implements.  If that class does not implement the abstract methods from its interfaces, then that class is abstract.

The ability for a class to implement a number of interfaces amounts to multiple interface inheritance

Java's more restricted form of multiple inheritance is a much safer form than the more general multiple inheritance supported by C++. The trickiest area of multiple inheritance is when you are trying to inherit multiple implementations; if you restrict multiple inheritance to multiple interface inheritance, most of the problems go away.

Interface "Contracts"

One way to look at an interface is to consider it to be a "contract".  Any class that specifies it implements that interface is "contractually obligated" to implement all the (abstract) methods specified in the interface.

Such a "contract" only specifies that there be an implementation, not how it might be implemented.

 

This page was last modified on 02 October, 2007