OO Programming with Java


Abstraction

  1. Thus, Group (and Person) can be simplified by using the new class:
public class Group extends NamedElement {
  public Group(String name) { super(name); } // call the superclass constructor
  // simplification: "name" is inherited and "copied" by the compiler
  private NamedElement[] elements = new NamedElement[50];
  private int size = 0;
  public void add(NamedElement element) {
    elements[size] = element;
    size++;
  }
  public String toString() {
    String str = super.toString();
    for(int i=0; i<size;i++)
      str += elements[i].toString()+"\n";
    return str;
  }
}

The preceding code proposes too another illustration of the prolymorphism: a group is composed of named elements that can be sustituted by any sublass; so, a person can be added to the group ... or another sub-group can be added !
This is an illustration of what is called the Composite design pattern.


UML/SysML class diagram


8 - 14