SDSU CS 596 Java Programming
Fall Semester, 1998
Interfaces
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 05-Oct-98

Contents of Doc 8, Interfaces


References


The Java Programming Language , 2 ed., Arnold & Gosling, Chapter 4


Doc 8, Interfaces Slide # 2

Interfaces


Let B & C be classes. Assume that we make A the parent class of B and C so A can hold the methods and fields that are common between B and C.



Sometimes a method in B is so different from the same method in C there is no shared implementation possible in A. We can make the method and A an abstract classes. The methods in A then indicate which methods must be implemented in B and C. A can act as type, which can hold objects of type B or C.

Sometimes all the methods of B must be implemented differently than the same method in C. Make A an interface.

Interfaces can specify public methods but can have no implementation of methods. Thus, one can not make an object from an interface. Interfaces can have fields. All fields in an interface are final and static even if they are not explicitly declared as such. Interfaces have the same access levels as classes, public and package.

A class can implement more that one interface. If a parent class implements an interface, its child classes automatically implement the interface.

An interface, like a class, defines a type. Fields, variables, and parameters can be declared to be of a type defined by an interface.

Listen Here!S-sept30 8mins, Q-oct1 5mins Doc 8, Interfaces Slide # 3
Interface Example
This example shows the syntax of declaring an interface and a class implementing the interface. The class must either implement all the methods in the interface or be an abstract class.

public interface Door {
   public void open();
   public void close();   
}
public class CarDoor implements Door {
   public void open() {
      System.out.println( "Enter the car" );
   }
   public void close() {
      System.out.println( "Look out, closing the door");
   }
}
class TestDoor {
   public static void main( String[] args ) {
      Door automatic = new CarDoor();
      automatic.open();
      CarDoor direct = new CarDoor();
      direct.open();
   }
}
Output
Enter the car
Enter the car

Doc 8, Interfaces Slide # 4
Abstract Class implementation of Interface

This example shows a class that implements only some of the methods declared in an interface. The class "CarDoor" must then be declared abstract. Objects can not be created of the "CarDoor" class. A subclass must implement the missing method.

public interface Door {
   public void open();
   public void close();
}
abstract class CarDoor implements Door {
   public void open() {
      System.out.println( "Enter the car" );
   }
}


Listen Here!S-sept30 2mins Doc 8, Interfaces Slide # 5

All Interface Methods are Public


It is a compile error to declare a method as private or protected in an interface. Any method without an access level in an interface is a public method. This can be confusing so always declare methods in an interface as public.

Example 1
public interface BadAccess {
   public void A();
   protected void B();   // compile error
   void C();                  // looks like package, but is public
   private void D();      // compile error
}

Example 2
package whitney;
public interface GoodAccess {
   public void A();
   void C();   
}
package test;
import whitney.GoodAccess;
class BadImplementation implements GoodAccess {
   public void A() {}
   void C() {}         // Compile error, wrong access level
}
class GoodImplementation implements GoodAccess {
   public void A() {}
   public void C() {}
}

Listen Here!S-sept30 5mins Doc 8, Interfaces Slide # 6

Interfaces and Static Fields

Fields can be declared in an interface. All fields are public static and final. Declaring a field to be any other access level except public is a compile error. If no access level is given, it defaults to public. If a field is not explicitly declared to be static or final, the field is still static and final. You may be tempted to use interfaces as a place to put program contants. Avoid this. Usually a constant belongs to some abstraction. This abstraction should be represented by a class. Place the constant in that class.

interface WithStatic    {
   public static final int EXPLICIT = 42;
   public static int IS_FINAL =    12;
   public int IS_FINAL_AND_STATIC = 3;
   protected int COMPILE_ERROR = 4;
   
   public int NO_VALUE_COMPILE_ERROR;
}
class Radio implements WithStatic {
   public void AM() {
      System.out.println( IS_FINAL );
   }
}
class Test   {
   public  static  void  main( String  args[] )    {
      System.out.println( WithStatic.EXPLICIT );
      System.out.println( Radio.EXPLICIT );
      Radio megaBass = new Radio();
      System.out.println( megaBass.EXPLICIT );
      megaBass.AM();
   }
}

Listen Here!S-sept30 1min, Q-oct1 7mins Doc 8, Interfaces Slide # 7

Extending Interfaces

One interface can inherit from another interface. Interfaces support multiple inheritance.

interface Door {
   public void open();
   public void close();
}
interface LockableDoor extends Door {
   public void lock();
   public void unlock();
}
class CarDoor implements LockableDoor {
   private boolean isLocked = false;
   public void open() {
      if ( !isLocked)
         System.out.println( "Enter the car" );
   }
   public void close() {
      System.out.println( "Look out, closing the door");
   }

   public void lock() {   isLocked = true;   }
   public void unlock() {   isLocked = false;   }
}
class TestDoor {
   public static void main( String[] args ) {
      Door automatic = new CarDoor();
      automatic.open();
      automatic.lock();      //Compile error
      LockableDoor better = (LockableDoor) automatic;
      better.lock();         //OK
   }
}

Listen Here!S-sept30 2mins Doc 8, Interfaces Slide # 8

Multiple Interfaces

A class can implement multiple interfaces. This example uses the LockableDoor and Door interfaces from the last slide.

interface Alarm {
   public boolean isAlarmed();
   public void turnAlarmOn();
   public void turnAlarmOff();
}
class CarDoor implements LockableDoor, Alarm {
   private boolean isLocked = false;
   private boolean isAlarmOn = false;
   public boolean isAlarmed() {
      return isAlarmOn;
   }
   
   public void turnAlarmOn()   {   isAlarmOn = true; }
   
   public void turnAlarmOff() {   isAlarmOn  = false; }
   
   public void open() {
      if (isAlarmOn ) 
         System.out.println( "Sound the alarm");
      else if ( !isLocked)
         System.out.println( "Enter the car" );
   }
   public void close() {
      System.out.println( "Look out, closing the door");
   }
   public void lock() {   isLocked = true;   }
   public void unlock() {   isLocked = false;   }
}


Listen Here!Q-oct1 1min Doc 8, Interfaces Slide # 9

Interfaces and Inheritance

A class can implement multiple interfaces and extend one other class. This example uses the LockableDoor, Door and Alarm interfaces from the last two slides.

class CarPart   {
   private int partID;
   private float weight;
   private float cost;
   public void aMethod()   {
      System.out.println( "This is a car part method" );
   }
}
class CarDoor    extends CarPart 
                     implements LockableDoor, Alarm {
   private boolean isLocked = false;
   private boolean isAlarmOn = false;
   public boolean isAlarmed() {
      return isAlarmOn;
   }
   
   public void turnAlarmOn()   {   isAlarmOn = true; }
   
   public void turnAlarmOff() {   isAlarmOn  = false; }
   
   public void open() {
      if (isAlarmOn ) 
         System.out.println( "Sound the alarm");
      else if ( !isLocked)
         System.out.println( "Enter the car" );
   }
   public void close() {
      System.out.println( "Look out, closing the door");
   }
   public void lock() {   isLocked = true;   }
   public void unlock() {   isLocked = false;   }
}

Listen Here!S-sept30 7mins, Q-oct1 3mins Doc 8, Interfaces Slide # 10

Interfaces and Name Conflicts


What happens when a class implements two interfaces, which each define a method or field with the same name?

public interface Left {      public void truth();  }
public interface Right {   public boolean truth(); }
class Congress implements Left, Right {
   public ????  truth() {   }
}
Method Name Conflicts

If a class implements two interfaces that each have a method with the same name, say foo, then

If both foos have different signatures, then the class implements two overloaded foo methods
If both foos have the same signature and the same return type, then the class implements only the one foo
If both foos have the same signature and different return types, then the class can not implement both interfaces
If both foos have the same signature, same return type but differ in the types of exceptions they throw, then the class implements only the one foo, but it must contain the exceptions both foo's
If a class implements two interfaces that each have a field with the same name, say bar, then the class has to use the full names for the fields.

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 27-Sep-98