[top] [up] [next]

Introduction


Object-oriented concepts

Functional programming solves problems by defining functions, logical programming solves problems by using logical rules, object oriented programming solves problems by defining and using objects. Objects are created, used and deleted by object oriented programs. Collections of similar objects are grouped into classes. A class defines the interface and data attributes of all objects of the class. An interface is a collection of functions which can be executed by each object of a class, the correct term for a function of an object is a method. The data attributes are the collection of properties stored by each object of a class.

Like in the real world an object is a "thing" that can be distinguished from other objects, each object is a separate entity. Each object can contain several properties, the containers for the properties are called attributes, each attribute each is given an unique name. For instance, a person object can have two attributes called 'Name' and 'Address'. The values of attributes can change but the number and names of attributes cannot!

Methods are the actions an object can perform, each method is identified by a name. A method can be public or private, if a method is public it can be accessed from outside the class, if a method is private it can only be called by another method of the same class.
Each object belongs to a single class. An object of a class is also called an instance of a class. The functionality of a class can be re-used by inheritance. Through inheritance a class is used as a base class (or superclass) for one or more subclasses, the subclasses can add extra methods and/or attributes (specialization). To promote re-use of code it is also possible to declare classes as abstract, an abstract class is a class which cannot be used to create instances, it can only be uses as a container of source code that is re-used by subclasses.

This is very short introduction of the most important concepts of object oriented programming, to learn more there are many excellent books about object oriented design and programming.

OO prolog

By using object oriented prolog it is possible to combine the best of both worlds; logical programming and object oriented programming. Logical programming lets the developer concentrate on a problem and object oriented programming promotes reuse, information hiding and a clear design. All the principles of prolog still apply to the object oriented extensions, backtracking with methods is the same as with prolog clauses, finding a correct method to call is still done by matching and the logical and immediate database views are the same for instances as for clauses.

Prolog is a type free language but defining a class adds a type to the language, because of that the prolog interpreter displays the type. For instance; an object of the class person is called p1 and it has one attribute with the value 'ron'. The interpreter will display the instance as 'person.p1(ron)'. The type of instance is displayed first, then a single dot which is followed by the name of the instance and the values of the attributes. If a module is saved to a file then the type information is also added so the interpreter can determine the class to which an instance belongs if the instance is loaded again.

What also differs from normal prolog is that values of attributes are persistent, i.e. they can survive backtracking. But like with normal prolog values of attributes can be altered while an attempt to redo a goal is active. To make values persistent a new operator was added, the assignment operator.

[top] [up] [next]

 

info@trinc-prolog.com