CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Interfaces

Interfaces
Interfaces
Use inheritance with interfaces

How to use inheritance with interfaces

  • An interface can inherit one or more other interfaces by specifying the inherited interfaces in an extends clause.
  • An interface can't inherit a class.
  • Unless it's defined as abstract, a class that implements an interface must implement all the methods declared by the interface and by any inherited interfaces.
  • A class that implements an interface can use any of the constants declared in the interface or in any inherited interfaces.

The syntax for declaring an interface that inherits other interfaces

public interface InterfaceName
    extends InterfaceName1[, InterfaceName2]...
{
    // the constants and methods of the interface
}

Example 1: A ProductReader interface

public interface ProductReader
{
    Product getProduct(String code);
    String getProductsString();
}

Example 2: A ProductWriter interface

public interface ProductWriter
{
    boolean addProduct(Product p);
    boolean updateProduct(Product p);
    boolean deleteProduct(Product p);
}

Example 3: A ProductConstants interface

public interface ProductConstants
{
    int CODE_SIZE = 4;
    int DESCRIPTION_SIZE = 40;
}

Example 4: A ProductDAO interface that inherits three interfaces

public interface ProductDAO
    extends ProductReader, ProductWriter, ProductConstants
{
}
Previous | Code | Implement | Inherit a class | Use an interface as a parameter | Use inheritance with interfaces | Next