OO Programming with Java


Inheritance

  1. To improve the display, the toString operation can be overloaded (i.e. rewritten). The same thing can be done, if one needs for instance to compare names of groups (not their memory address/pointers):
public class Group {
  public String  toString()           { return this.getName(); }
  public boolean equals(Object other) {
    if (other instanceof Group) 
      return (((Group)other).getName().equals(this.getName())); 
    return false;
  }
  ...
}

In a similar manner, the toString operation of the RestrictedGroup class can be overloaded to display capacity:

public class RestrictedGroup extends Group {
  public String toString() {
    StringBuffer buf = new StringBuffer();
    buf.append( super.toString() ); // display superclass information
    buf.append(" (");
    buf.append( this.getCapacity() );
    buf.append(")");    
    return buf.toString();
  }
  ...
}

UML/SysML class diagram


6 - 14