Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.6. Interfaces

4.6. Interfaces
Last updated: 23 January 2013.

An interface is like an abstract class except that none of its methods are implemented. Basically, an interface only contains method signatures and constants. Variables are not allowed in interfaces. As an example, the interface AircraftInterface shown below contains two constants and two method signatures:

public interface AircraftInterface {

   
int MINIMUM_TAKEOFF_DISTANCE = 0;
   
int MAXIMUM_CREW_COUNT = 30;

   
boolean land();
   
boolean takeOff();

}

The keyword final is implicit and can therefore be omitted in the constants declarations. Constants are also implicitly static and can therefore be accessed directly via the name of the interface.

Note that the methods declared in interfaces are implicitly abstract. Consequently, the interface declaration shown above is the same as the following:

public interface AircraftInterface {

   
static final int MINIMUM_TAKEOFF_DISTANCE = 0;
   
static final int MAXIMUM_CREW_COUNT = 30;

   
abstract boolean land();
   
abstract boolean takeOff();

}


Similarly to an abstract class, an interface cannot be instantiated. Instead, an interface must be implemented by a class. The keyword implements allows you to declare a class that implements an interface. When a class implements an interface, it must implement all the methods of the interface. For example, the class Plane shown below implements the interface AircraftInterface:

public final class Plane implements AircraftInterface {

   
boolean land(){
         
// The implementation goes here
   
}

   
boolean takeOff(){
         
// The implementation goes here
   
}

}

A class can implement multiple interfaces. In that case, the implemented interfaces are separated by commas as shown below:

public final class MyClass implements Interface1, Interface2, Interface3 {

   
// The methods in the 3 interfaces must be implemented

}

Note that a class can at the same time extend another class and implement interfaces:

public final class MyClass extends MySuperClass implements Interface1, Interface2, Interface3 {

   
// The methods in the 3 interfaces must be implemented

}

An interface can extend one or more other interfaces. For example, the interface Interface1 shown below extends the interfaces Interface2 and Interface3:

public interface Interface1 extends Interface2, Interface3 {

   
// constants and method signatures of Interface1

}

Naturally, a class that implements the interface Interface1 must implement the methods of all 3 interfaces.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.6. Interfaces
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.7. Polymorphism

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7