16. Inheritance Diagrams

In order to explain how an object oriented program is put together, techniques have been developed to document classes and their relationship to one another. One such technique are UML diagrams. Short for 'Unified Modeling Language' diagrams.

Consider the Box example

class Box() {
private var width
private var height


  public function Box() {
    var myBox = new Box
  }


  public setHeight(x) {
    myBox.height = x
  }

}

As an UML diagram it can be shown like so:

UML diagram

The class is drawn as a box. At the top is the name of the class. Then the next section declares the data within the class, including the data type of each variable. This is followed by a list of all the methods within the class along with the parameter data types each method needs including any returned parameters. Someone reading this, quickly understands what the class requires and how to call its methods.

Going a little further, a derived class is drawn like so:

UML Derived class

Where the arrow shows inheritance. In english you would say ColouredBox is a subclass of Box

Slightly more complicated, consider an object oriented program having three classes, namely a superclass called Employee and two derived classes or subclasses called FullTimeEmployee and PartTimeEmployee

Inheritance diagram

The diagram shows that the superclass called Employee has two data properties (or 'attributes') called name and started. There are also two methods that can set these two properties.

A derived class called FullTimeEmployee inherits the properties and methods of Employee and in addition defines a property called pensiontier which defines the kind of pension this full time employee is on.

Another derived class called PartTimeEmployee inherits from Employee and defines a property called hoursworked along with a method of setting it.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: UML Diagrams

 

 

 

 

Copyright © www.teach-ict.com