An interface is a collection of method declarations.
A class-like concept.
Has no variable declarations or method bodies.
• Describes a set of methods that a class can be forced to implement.
• Can be used to define a set of “constants”.
• Can be used as a type concept.
• Can be used to implement multiple inheritance like hierarchies.

Combining Multiple interfaces

interface InterfaceName {
// "constant" declarations
// method declarations
}
// inheritance between interfaces
interface InterfaceName extends InterfaceName {
...
}

interface InterfaceName extends ClassName { ... }
// extends multiple interfaces (multiple inheritance like)
interface InterfaceName extends InterfaceName1, InterfaceName2
{ ...
}
Interfaces and Classes

// implements instead of extends
class ClassName implements InterfaceName {
...
}
// combine inheritance and interface implementation
class ClassName extends SuperClass implements InterfaceName{
...
}// multiple inheritance like again
class ClassName extends SuperClass
implements InterfaceName1, InterfaceName2 {
...
}
// multiple inheritance like
class ClassName implements InterfaceName1, InterfaceName2{
...
}
class ClassName extends InterfaceName {...}

Semantic Rules for Interfaces

• Type
 An interface can be used as a type, like classes
 A variable or parameter declared of an interface type is polymorph
 Any object of a class that implements the interface can be referred by the variable

• Instantiation

 Does not make sense on an interface.

• Access modifiers

 An interface can be public or “friendly” (the default).
 All methods in an interface are default abstract and public.
 Static, final, private, and protected cannot be used.
 All variables (“constants”) are public static final by default
 Private, protected cannot be used.

The Cloneable Interface

• A class X that implements the Cloneable interface tells clients that X objects can be cloned.
• The interface has no methods
 
An “empty” interface

• Returns an identical copy of an object.

A shallow copy, by default.
A deep copy is often preferable.

• Prevention of cloning
 
Necessary if unique attribute, e.g., database lock or open file reference.
Not sufficient to omit to implement Cloneable.
Subclasses might implement it.

clone method should throw an exception:

CloneNotSupportedException

The Cloneable Interface, Example

public class Car implements Cloneable {
// instance variables
private String make;
private String model;
private double price;
// give reasonable values to instance variables
public Car(String make, String model, double price){
this.make = make;
this.model = model;
this.price = price;
}

public Object clone(){
return new Car(this.make, this.model, this.price);
}

public Car clone(){
return new Car(this.make, this.model, this.price);
}
}

The Serializable Interface

• A class X that implements the Serializable interface tells clients that X objects can be stored e.g, on file.
• The interface has no methods

public class Car implements Serializable {
// rest of class unaltered
}

• Class must implement the Serializable interface
• Uses
Output: ObjectOutputStream
writeObject()
Input: ObjectInputStream
readObject()
• All relevant parts (the web of objects) are serialized.
• Lightweight persistence
used in RMI (send objects across a network)
 used in JavaBeans
• Similar functionality in C#, PhP, Python, Perl