next up previous contents index
Next: Constraints Up: Defining Classes Previous: Order of declarations

Using inheritance

DJ retains the object-oriented feature of Java. A class A can extends another class B. In this case, class A is called a subclass of B, and class B is called the super class of A. By using inheritance, we can write the HelloWorldButton as follows:
       class HelloWorldButton extends Button {
             text=="Hello World!";
       }
The class HelloWorldButton inherites all attributes from Button including the four default attributes: x, y, width and height. Let hw be an instance of HelloWorldButton. For the previous definition, we refer to the size of the button as hw.bt.size. For this declaration, however, we refer to the size of the button as hw.size. The attribute size in HelloWorldButton is inherited from its super class Button. In our previous example, hw may be larger than the button. In this example, however, because of inheritance, hw has exactly the same size and position as the button.  

Neng-Fa Zhou
1999-02-16