| 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
- References
- Interfaces
- Extending Interfaces
- Multiple Interfaces
- Interfaces and Inheritance
- Interfaces and Static Variables
- Interfaces and Name Conflicts
The Java Programming Language, Arnold and Gosling, 1996, Chapter 4
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
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
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
}
}
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; }
}
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; }
}
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();
}
}
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.