Inheriting from an external class

Use the CG::Class::UseAsExternal property to inherit from an external class.

About this task

To inherit from a class that is not part of the model, set the CG::Class::UseAsExternal property for the superclass to Checked. This setting prevents code from being generated for the superclass.

To generate an #include of the superclass header file in the subclass, do one of the following actions:

Another way to inherit from an external class is to exclude the external class from the code generation scope. For example, if you want a class to extend the Java class javax.swing.JTree without actually importing it:

Procedure

  1. Draw a package javax.
  2. Draw a nested package swing inside javax.
  3. Draw a class JTree inside the swing package.
  4. Exclude the javax package from the component (do not make it one of the selected elements in the browser). This action prevents the component from generating code for anything in the javax package.

Results

This gives the rest of the model the ability to reference the JTree class without generating code for it. In this way, a class in the model (for example, MyJTree) can inherit from javax.swing.JTree. If the subclass is public, the generated code is as follows:

import javax.swing.JTree;
...
public class MyJTree extends JTree {
...
}

If you need a class to import an entire package instead of a specific class, add a dependency (see Dependencies) with a stereotype of «Usage» to the external package, in this case javax.swing. The generated file is then included the following line:

import javax.swing.*

For more information about using external elements, see Visualization of external elements.


Feedback