Section 9.4. Interfaces

   

9.4 Interfaces

An interface is a lot like an abstract class. The chief difference is that an interface cannot have any code in it. You might think of it as the class-level version of an abstract method. Remember that an abstract method is declared like this:

 public abstract double getSpeed(); 

It just ends with a semicolon and no code. An interface is similar, and it has a usefulness like an abstract method's.

The real purpose of interfaces is to prevent multiple inheritance. Other programming languages, including C++, allow you to subclass from more than one immediate parent. It would be like writing this in Java:

 public class A extends B, C ... 

You don't need to do this in Java, and you cannot. You can inherit from more than one class, of course, just by virtue of the fact that you can inherit from a class that is itself a subclass. Then you get everything from the parent and the grandparent. But you can only have one immediate superclass. If you organize your class hierarchy correctly, then this shouldn't be a problem. Sometimes systems get very complex, however, and you would like to be able to do this sort of thing.

An interface is a special kind of class declaration that allows you to declare constants and method declaration, but not the implementation of the methods . An interface can contain no code.

The bare structure of an interface looks like this:

 public interface MyInterface {     void myMethod(); } 

As you can see, it is much like a class, but you write "interface" instead of "class."

Note

All methods in an interface are public by default. You therefore do not need to specify a public accessibility modifier.


When you want to use an interface in a class, you use the implements keyword where you would put extends if this were an implementation of an abstract class:

 public class SomeClass implements SomeInterface 

You can implement an interface and extend a superclass in the same class:

 public class SomeClass extends ParentClass implements      SomeInterface 

Interfaces are useful when you want to define what a class must do, but not how it should do it. Abstract methods, covered in the last section, are examples of this ”they provide a signature and not the implementation. This forces any subclasses to define the implementation for the abstract methods inherited.

An interface can be implemented and used by any number of classes. A single class can implement as many interfaces as it likes. Interfaces provide another key aspect of polymorphism, because one interface gives you multiple methods.

Note

If you have ever written a ColdFusion CFX tag in Java to extend ColdFusion, you have used the Allaire interfaces.


Interfaces must be declared public , or the access modifier must be omitted. Declaring an interface as public means that omitting the access modifier causes the access level to be set to default , which, as you may recall, means package-level access.

Every class that implements an interface must implement all of its methods, each of which are implicitly public . Because there is no implementation in an interface, there cannot be instance variables. Any variables in an interface must essentially be constants. These are implicitly public , static , and final , and must be initialized .

An interface has the following structure:

 accessModifier interface name {   type aVariable = value;   returnType methodName(argumentList); } 

The way to implement an interface is to use the implements keyword in your class definition. In the class that implements the interface, you must write the bodies of the interface's methods. These methods must be declared public , and their signature must exactly match the signature of the interface method.

In this example, we'll look at probably the simplest interface and implementation one could make.

9.4.1 MakeHelloInterface.java

 // use implements keyword to specify interface  public class MakeHelloInterface implements HelloInterface {     public static void main(String[] args) {         MakeHelloInterface mhi = new MakeHelloInterface();         System.out.println(mhi.sayHello());     }         // implement this method from the Hello interface     public String sayHello() {             // this is a constant, and is initialized in             // the interface         return HELLO;     } } 

9.4.2 HelloInterface.java

 public interface HelloInterface {         // a constant field     final String HELLO = "Howdy ho, neighbor!";         // method to be implemented    public String sayHello(); } 

As we see above, you can call a method declared in an interface on an object. It is important to note that the same object can also be used to store references to any object that implements another interface.

If a class declares that it implements an interface, but does not implement all of the methods of the interface, it must be declared abstract . You cannot create objects of that abstract class, but this can still be a useful approach, as the abstract class may be subclasses, and the remaining implementation can take place in the subclass.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net