Object Oriented Languages

One of the most popular programming paradigms in recent years is object-oriented programming. In an object-oriented programming language, less emphasis is placed upon the flow of execution control. Instead, the program is viewed as a set of objects interacting with each other in defined ways. Objects are 'instances' of a class (type of object). Classes are templates or blueprints for building objects. Each class is designed to represent some functional grouping of data and processing within the program. A class can be thought of as an extension of the structures which are used in a language like C (see the section entitled 'procedural languages'). A class is essentially a structure with subroutines. That is, a class is not only a container for data, but it also provides functions (called class methods) which manipulate that data in precisely defined ways.

One of the most powerful features of object-oriented languages is inheritance. This allows classes to be defined as subclasses of others. A subclass takes on all of the data and methods of all of its 'ancestor' classes, and can define new data and methods. Inheritance allows the code for a program to be organized into a hierarchy of classes and subclasses, from most abstract to most specific. In this way functionality which is common to a great many types of objects may be defined in just one location for all of these. This feature greatly increases the modularity and reusability of code, which can ultimately save time programming and maintaining complex applications.

Object-oriented languages typically also provide a mechanism to control access to data and methods. Just as procedural languages provide a scope which can restrict access to variables, so an object-oriented language can restrict access to object data and methods. Access controls can limit how other parts of a program can interact with an object. A programmer could protect the integrity of data by limiting access to a particular data variable only to the object which contains it. Other portions of code could then only alter that data by invoking the accessible methods of that object. This prevents code in some distant unrelated part of the program from corrupting the data.

This compartmentalization of functionality also allows for an easy way to divide programming work among a team of programmers. The design of each class and the access restrictions on it create a kind of 'programmer's contract', in which the programmer responsible for the internal functionality of the object implicitly agrees that her code will provide a certain functionality if the programmer who is making use of that functionality complies with the framework which makes that functionality accessible. This kind of programming by contract is naturally quite well adapted to the commercial software industry, in which code produced by one company may be used by many others. In this environment the 'contract' implicit in the software design, while in no sense a legal contract, helps to shape the technical relationship between the various companies involved.

The following example Java program generates the same population growth model sequence as the C program example given above. Due to the nature of the Java language, the code is split between two files.

* @throws ArraylndexOutOfBoundsException if there is no nth element

* @throws ArraylndexOutOfBoundsException if there is no nth element


* This class extends the simple array type to implement a

* sequence of population growth figures */

public class PopN extends SimpleArray {

private double initial_pop;

private double growth_factor; /**

* Constructor initializes the population growth figures

* @param size the number of steps of population growth

* @param pO the initial population

* @param r the growth factor

public PopN( int size, double pO, double r )

// calls the constructor method for SimpleArray super( size );

// initialize our constants this.initial_pop = pO;

this.growth_factor = r;

// generates the population figures for( int t = 0; t < size; t++ ){

double current_pop = pO * Math.exp( r * t ); this.set( t, current_pop );

public String toStringO {

String str = "showing the first "+ this.sizeO; str += " population growth steps\n";

str += "from a starting population of "+ this.initial_pop; str += " with a growth factor of "+ this.growth_factor +"\n" f or ( int t = 0; t < this.sizeO; t + + ){

str += "P("+ t +") = "+ this.get( t ) +"\n";

return str;

* The starting point of our program */

// our two populations

PopN popl = new PopN( 20, 2.0, 0.5 ); PopN pop2 = new PopN( 20, 42.0, 0.02 5 ); // print out the results System.out.println( popl.toString() ); System.out.println( pop2.toString() );

This example shows how a generic class, SimpleArray, can be extended to a very specific purpose. The parent class provides some simple functionality for creating and manipulating an array of real-valued numbers, and the inherited PopN class utilizes this to store the sequence of values for the population growth model. Notice also that access to this data is restricted to a couple of methods known as accessor methods.

Java and C++ are the most commonly used object-oriented languages; however, many other languages also use objects to some degree or another. Many languages which are not typically thought of as object oriented, such as Perl, do provide the capabilities to use objects if so desired. Others, such as Java, force all code to be defined in terms of objects. Many languages which were not originally object oriented, have had object-oriented revisions in recent years, such as DELPHI, which is an object-oriented version of PASCAL. Most object-oriented languages are also procedural languages. Some languages, such as Ocaml (Objective Caml) or F#, attempt to integrate the features of object-oriented programming with functional programming.

Was this article helpful?

0 0
Project Earth Conservation

Project Earth Conservation

Get All The Support And Guidance You Need To Be A Success At Helping Save The Earth. This Book Is One Of The Most Valuable Resources In The World When It Comes To How To Recycle to Create a Better Future for Our Children.

Get My Free Ebook

Post a comment