Language Reference/Basic Concepts/Object System
External View
This section clarifies the class-related notions in Visual Prolog at a conceptual level. It does not discuss syntax or implementation details.
The class concept in Visual Prolog is based on three semantic entities:
- objects
- interfaces
- classes
Object
An object is a set of named object member predicates together with a set of supported interfaces. Objects also have state, but this state can only be observed and changed through member predicates; the state is therefore encapsulated in the object.
Interface
An interface is an object type. It has a name and defines a set of named object predicates.
Interfaces form a supports hierarchy (a semi-lattice) rooted at the object interface. If an object has a type denoted by some interface, it also has the types of all supported interfaces. Thus the supports hierarchy is a type hierarchy: an interface is a subtype of each interface it supports.
Class
A class is a named object factory; it constructs objects that correspond to a given interface. If a class constructs objects of interface iii, then its objects are "iii objects". All objects constructed by the same class share the same definitions of the object member predicates, but each object has its own state; the predicate definitions belong to the class, while the state belongs to each object.
The set of object member predicates a class defines is the union of the predicates declared (transitively) in its interfaces. If the same predicate is declared in multiple interfaces, the class provides one common definition; this is only valid if the intended semantics coincide. Interface support must be specified explicitly.
Module
A class need not construct objects. A class without a construction type acts as a module: it may have only class members and class state.
Identity
Every object is unique. Even if two objects currently have identical state, they are not identical: one can be changed without affecting the other. We never access object state directly; we access it through references, and many references may denote the same object. Classes and interfaces are also unique and identified by name (with namespace); duplicate names are not allowed within a program. Structural equality does not imply identity for objects, classes, or interfaces.
Internal View
This section complements the external view with internal aspects: how declarations and implementations are organized. From a programmatic point of view, classes are central (they contain the code). Interfaces have mainly static importance and have no direct runtime representation; objects are dynamic and exist only when the program runs.
A class has a declaration and an implementation. The declaration states the public accessible parts of the class and of the objects it constructs; the implementation defines what the declaration introduces (predicates are implemented by clauses, by inheritance, or resolved to external libraries). A class declaration is purely declarative.
An implementation can declare additional private entities (domains, predicates, etc.). Object state is stored as facts in the implementation; such facts are per-object, while class facts are shared across all objects of the class. Facts are declared only in implementations and are not directly accessible from outside. Implementations may also (privately) support more interfaces than stated in the declaration.
Code Inheritance
Code inheritance occurs only in class implementations. Visual Prolog supports multiple inheritance via an inherits section in the implementation. The classes you inherit from are parent (super) classes; the inheriting class is the child (sub) class. A child class can only access its parent classes through their public interfaces; it receives no special privileges.