Abstract classes and interfaces, declaring a set of public methods, give an important extension to the way we can design programs. The core framework of a program can be designed around a set of abstract classes and interfaces. These specify the structure of a program, the core set of behaviours it should provide and the interfaces by which components in the framework fit together.
With the abstract framework in place, inheritance can be used to specialize each abstract class, creating a matching collection of concrete subclasses. The concrete classes provide detailed implementation and specific behaviour for the program.
Interfaces then provide an additional way in which objects of those classes can work with one another without their classes having to be related by inheritance. Providing a class supports a particular interface, its objects can be used wherever that interface is specified.
This gives two major advantages:
Reuse is a recurring theme with object-oriented programming. Classes form the basic building block of reuse as they provide encapsulated objects which are relatively easy to reuse in many different situations. Java fully exploits this as it comes with a large class library providing many reusable classes.
However, reuse is not limited to individual classes. Collections of classes also provide reusable units in the form of frameworks and components. Indeed, a powerful force that is driving the development of programming systems is the growing support for pluggable components (see Figure 4.2). A component is typically implemented by a small collection of classes, with one class acting as an interface to the component.
Rather than designing a program from scratch, it can be built out of a set of pre-defined components which are linked together with small amounts of new code. This approach is termed component-based design, though in the WWW environment it is called object scripting.
The connections between components are enabled using the mechanisms of inheritance, interfaces and dynamic binding. Users of the component make use of the public interface to call the component's methods but need not be aware of the details of the component implementation.
An important feature of pluggable components is that new components can be created and added without having to change the users of the components (they carry on using the existing interface). Users do not even need to know exactly what kind of component they are using, only that it supports the right interface. Note, 'the right interface' not only means the right set of operation names but also that the expected behaviours must match the requirements, so the different components must all behave in the way expected.
Java provides a standard mechanism for constructing and using components which is know as 'JavaBeans'.
Copyright © 1997 Russel Winder and Graham RobertsLast updated: 6 Oct 1997