ObjectOrientedProgramming

From GIS CS4
Jump to: navigation, search

Object Oriented Programming (OOP)

Object Oriented programming (OOP) is a programming paradigm (or style) that represents concepts as "objects".

Object-oriented programming is an approach to designing modular, reusable software systems.

The goals of object-oriented programming are:

  • Increased understanding.
  • Ease of maintenance.
  • Ease of evolution.

An object has data properties (member variables).

An object also has associated methods (member functions).

Class

In OOP a class is a template (type definition) for creating an object.

It is a common convention to name a class commencing with a capital letter.

A class has a special function known as the constructor. The constructor is used to create an object of that type. The constructor function usually has the same name as the class.

Instance

When an object is created by a constructor of the class, the resulting object is called an instance, and the member variables specific to the object are called instance variables.

Inheritance

Classes are created in hierarchies. A new class can be derived from another class, e.g the more specific class Circle, can be derived from the more general class Shape.

The first, more general class is known as the base class, parent class or super class.

The second, more specific class is known as the derived class, child class or sub class.

Inheritance allows the properties and methods in a class to be reused by other classes in the class hierarchy.

That means less programming is required when adding functions to complex systems.

The ability to reuse existing objects is considered a major advantage of object technology.

Summary of JavaScript OOP Syntax

  • Your class name should begin with a capital letter.
  • Your property names should begin with a lowercase letter.
  • Keep all definitions for a given class together i.e. separate from other classes or program code.
  • Put all program declarations and code separate from your class definitions, i.e. below.
///////////////////////////////////////////
//BaseClass Object/////////////////////////

// define class name and constructor method
var BaseClass = function(zParam1, zParam2)
{
    // declare and initialise properties
    this.property1 = zParam1;
    this.property2 = zParam2;
};

// define a class method
BaseClass.prototype.method1 = function() 
{
    // method1 implementation goes here
};

///////////////////////////////////////////
//DerivedClass Object//////////////////////

// define derived class 
var DerivedClass = function(zParam1, zParam2, zXParam1, zXParam2)
{
    // call base class constructor to initialise base class properties
    BaseClass.call(this, zParam1, zParam2);

    // initialise additional derived class properties
    this.xProp1 = zXParam1;
    this.xProp2 = zXParam2;
};

// initialise inheritance relationship between the derived class and the base class.
DerivedClass.prototype = Object.create(BaseClass.prototype);

// the derived class can have a method of the same name as the base class
DerivedClass.prototype.method1 = function() 
{
    // you can specifically call the method of the same name on the base class...
    BaseClass.prototype.method1.call(this); 

    // implement the method here... 
};

// define a new method on the derived class
DerivedClass.prototype.method2= function(zParam) 
{
    this.prop1 = zParam;     // modify a property of the base class
    this.xProp1 = 2*zParam;  // modify a property of the derived class
    this.method1();          // call a method
    // ...
};

///////////////////////////////////////////
//My Program///////////////////////////////

// Declare instance(s)
var myObject1 = new DerivedClass(val1, val2, 200, 100);
var myObject2 = new DerivedClass(2*val1, 2*val2, 2*200, 2*100);

//call methods
myObject1.method1();
myObject2.method2(500);