SDSU CS 535: Object-Oriented Programming & Design
Fall Semester, 1997
Doc 10 Interface

To Lecture Notes Index
San Diego State University -- This page last updated 01-Oct-97

Contents of Doc 10 Interface

    1. References
    2. Interfaces
      1. Extending Interfaces
      2. Multiple Interfaces
      3. Interfaces and Inheritance
      4. Interfaces and Static Variables
      5. Interfaces and Name Conflicts

References



The Java Programming Language, Arnold and Gosling, 1996, Chapter 4



Doc 10 Interface Slide # 1

Interfaces


Let A, B, C be classes, with A the parent of B and C



A will contain the common methods and fields of B and C

Some times a method (field) in B is so different from the same method (field) in C that it is not possible to have any implementation of the method in A
Make the method and A abstract!


Some times all the methods (fields) of B must be implemented differently than the same method (field) in C
Make A an interface


Interfaces can specify public methods but can have no implementation of methods

Interfaces can have variables, but they are static and final

Interfaces give a type of multiple inheritance


Doc 10 Interface Slide # 2
Interface Example
interface Door {
   public void open();
   public void close();   
}

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 10 Interface Slide # 3

Extending Interfaces

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
   }
}

Doc 10 Interface Slide # 4

Multiple Interfaces


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;   }
}


Doc 10 Interface Slide # 5

Interfaces and Inheritance


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;   }
}

Doc 10 Interface Slide # 6

Interfaces and Static Variables


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();
   }
}


Doc 10 Interface Slide # 7

Interfaces and Name Conflicts

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 foo' s have different signatures, then the class implements two overloaded foo methods
if both foo' s have the same signature and same return type, then the class implements only the one foo
if both foo' s have the same signature and different return types, then the class can not implement both interfaces
if both foo' s 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.


visitors since 01-Oct-97