Aggregation

The COM concept of aggregation allows an object class to use and enhance the services provided by another object type. In other words, aggregation is the same concept as Java’s inheritance, which is implemented through the extends keyword. Indeed, Java supports aggregation through the standard inheritance model; you simply extend an existing COM class to incorporate its functionality. Java’s ability to implement COM aggregation is limited, however. For instance, Java does not support multiple inheritance, whereas COM allows an aggregated class to have more than one parent. In addition, Java cannot create COM and ActiveX objects in the same way other applications do. And a COM object must explicitly be declared “aggregatable” if your Java code is going to declare it in an extends clause of a class definition.

Java can extend an existing COM class, even if the class was not written in Java. For example, consider an aggregatable ActiveX component named Ticker, which is a timer. Using the Java Type Library Wizard (see Chapter 7), I would generate a set of Java .class files for Ticker. Then, to add methods to Ticker, I would create a class named ScottTicker. Assuming that Ticker has already been registered by the Java ActiveX Wizard, I’d begin by creating the following class.

import Ticker.*;
 
public class ScottTicker
    extends Ticker.Ticker
{

    void halveInterval()
    {
        putInterval(getInterval() / 2);
    }

© 1997 by Scott Ladd. All rights reserved.