next up previous
Next: Object Oriented Programming and Up: The Object Oriented Approach Previous: Abstraction

The Object Oriented Approach

The key ideas of the object oriented approach are :

On of the main principles in the object oriented (OO) approach is that of abstraction, not of data structures and processes separately but both together. An object is a set of data structures and the methods or operations needed to access those structures.

Encapsulation of data structures and methods means that only the methods associated with the object can access the internal data structures. An object is a packaged item of information including the processes which manipulate it. Although we saw above that it is possible to create such packages in e.g. C, the language did not specifically support or enforce the encapsulation.

The objects in OO software are reusable components which may often be used in different applications. In OO environments, object libraries may be available for the programmer to build into solutions.

Generic objects representing classes can also be defined and objects representing subclasses can inherit the data structures and methods from the broader classes to which they belong. A particular instance of a class is represented by instantiating the data structure to the appropriate values.

A programmer may define the structure of an object to represent person and then a new object for employee. The employee object will inherit the data structures and methods from the person object. Note that this inheritance property is equivalent to the is_a relation in semantic nets.

Here is an example of encapsulation in C++. The data items are private and no external functions can access them, only the methods defined in the object. The methods, initAddress etc, are public and so can be used outside the object. The detail of the methods is not shown here.

struct Address{
    
      private:
          
         unsigned char streetNumber;
         char*         cityName;
         char*         streetName;
         char*         country;
         char          postcode[7];

      public:
         
         void initAddress(unsigned snumber, char* sname, char* cname,
                          char* scountry, char* pcode};
         void deleteAddress();
         void streetAddress(char* name);
         void cityAddress(char* name);

};

In C++, class is another name for a struct construct, but whereas the default for struct members is public, the default for class members is private. It is best to indicate these explicitly to avoid ambiguity. An example of class and inheritace is shown below in outline. Aircraft is a class which is a specialisation of Vehicle. The term public Vehicle after the class Aircraft, indicates this and aircraft then inherits the data structures and methods associated with Vehicle, as well as having some additional ones of its own.

class Vehicle{

    private:
       
        int weight;
        int xvel,yvel;
        int maxspeed;

    public:

        void initVehicle(...);
        void changespeed(...);
         .
         .
         .
};

class Aircraft: public Vehicle{
    private:
      
        int maxAltitude;
        boolean inAir;
  
    public:

        void initAircraft(.....);
         .
         .
         .
};

Messages

In OO approaches, objects communicate by sending messages to each other in order to invoke the required methods within the objects.



next up previous
Next: Object Oriented Programming and Up: The Object Oriented Approach Previous: Abstraction



Paul Lewis
2003