OO Programming with Java


Objects & Classes

  1. A class is a model for objects that shared common structure and operations.
    Structure is generally hidden what is called encapsulation, i.e. we don't need to know what is a motor engine to drive.
    As an illustration, the concept of a Group having a name and a set of Person can be specified by the following class (interface = specification of a set of operations usable by a client and realized by the developper):
public class Group {
  public Group(String name)                  { ... }
  public void     addPerson(Person person)   { ... }
  public boolean  hasPerson(Person person)   { ... }
  public void     setName(String name)       { ... }
  public int      getSize()                  { ... }
  public Person[] commonPersons(Group other) { ... }
}

Thus, many groups can be defined, e.g. in the main application/operation:

public static void main(String[] args) {
  Group g1 = new Group("Dance");
  Group g2 = new Group("Tennis");
  g1.addPerson(new Person("Bill")); g1.addPerson(new Person("Amy"));
  g2.addPerson(new Person("John")); g2.addPerson(new Person("Amy")); 
}

As an exercise, gives an possible implementation (i.e. code within brackets below) of the Group class.


UML/SysML class diagram

Question: what is the objects' model for g1/g2 ?


1 - 14