Google
 

 

up previous next contents
Up: 2. Day 2: The Previous: 2.10 Extending Classes Next: 2.12 Exceptions   Contents

Subsections

2.11 Interfaces

2.11.1 Introduction

Interfaces are a way to declare a type consisting only of abstract methods and related constants, classes, and interfaces. An interface in Java is an expression of pure design, whereas a class is a mix of design and implementation.

2.11.2 Objectives

Upon completion of this section, students will:
  • Describe an interface.
  • Define the differences between single and multiple inheritance.
  • Show how to implement an interface.
  • Describe the differences between interfaces and abstract classes.

2.11.3 An example interface

  • Java has single inheritance of implementation - you can extend only one class.
  • Java has multiple interface inheritance.
  • All methods in an interface are implicitly abstract.
  • Each class that implements the interface must implement all its methods.
  • Methods in an interface are always public.
  • Fields in an interface are always static and final.

2.11.3.1 Nested classes and interfaces

  • Nested classes and interfaces let you associate types that are strongly related to an interface inside that interface.
  • Any class or interface inside an interface is public.
  • Any classes nested inside an interface are also static.
  • Any interface nested inside a class can be public, protected, package-accessible, or private.

2.11.4 Single inheritance versus multiple inheritance

  • A new class can extend exactly one superclass.
  • The new class inherits the superclass's (a) contract and (b) implementation.
  • Some languages allow multiple inheritance - two or more superclasses.
  • Problem of multiple inheritance arises from multiple inheritance of implementation. (Usually related to a class that maintains state information.)

2.11.5 Extending Interfaces

  • Interfaces can be extended using the extends keyword.
  • Interfaces can extend more than one interface:
      interface Shimmer extends FloorWax, DessertTopping \{ \ldots
    
  • All methods and constants defined by FloorWax and DessertTopping are part of Shimmer.

2.11.5.1 Name Conflicts

  • A class or interface can be a subtype of more than one interface.
  • What happens when a method of the same name appears in more than one interface?

2.11.6 Implementing Interfaces

  • Most interfaces may have several useful implementations.
  • Enumeration is an interface.

2.11.7 Using an Implementation

  • Use an implementing class just by extending it.
  • Can also use a technique called forwarding - create an object of an implementing class and forward all the methods of the interface to that object.

2.11.8 Marker Interfaces

  • Do not declare any methods - mark a class as having some general property.
  • Define no language-level behavior.
  • All of the contract is in the documentation.

2.11.9 When to Use Interfaces

2.11.9.1 Two Important Differences Between Interfaces and Abstract Classes

  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

2.11.9.2 Interface or Abstract Class

  • These two differences usually direct the choice.
  • If multiple inheritance is important or even useful interfaces are used.
  • Abstract class lets you define some or all of the implementation.
  • Any major class you expect to be extended should be an implementation of an interface.