J3 Limited
 
Google
WWW J3Ltd
 
OO Introduced

The purpose of this document is to give a quick overview of what Object Oriented programming/design is about.

Object

Programming languages usually implement an object as a class.

An object holds methods to do something

An object holds members (data about itself).

Example:

A person object has properties (or members) such as:

  • Age
  • Height
  • Sex
  • Name
  • Surname
  • Title
  • IsMarried
  • IsRetired
  • Current thought

It can have methods such as

  • Wake up
  • Eat breakfast
  • Eat lunch
  • Exercise
  • Go to bed
  • Marry

Encapsulation

Encapsulation embodies an object's behaviour. For example the exercise method could change the person's weight property. Also changing a person's age property could also cause the IsRetired property to change.

Encapsulation is the term used to describe that a class encapsulates all aspects of an object. OO languages achieve this by way of methods and properties.

Inheritance

A class can inherit another "superclass". This allows a class to use the functionality in another class, and add its own bits on top. In this way we do not have to rewrite common code in all our class descriptions.

For example.

class man extends person

The man class inherits all the person characteristics. The person class is the topmost class in the inheritance hierarchy, and is known as a base class (the person class does not inherit from any other class).

The man class adds the following properties:

  • Wears a tie
  • Has a beard

We can also add methods such as

  • Go to the gent's

class Woman extends person

  • Wears a skirt
  • Go to the ladies'
  • marry

Since man and woman both "extend" person, they inherit all of a person's (base class) characteristics. They have an age, height, and they can eat breakfast....

Polymorphism

With inheritance it is possible to pass an object as an instance of a superclass. For example, we could pass a woman object to another object which expects a person object. Polymorphism allows the characteristics of the super class to be overridden. For example:

A priest object has a method: marryCouple(person1, person2). Where person1 happens to be a man, and person2 happens to be a woman. The body of the priest's marry method does the following:

  • calls marry on person1
  • calls marry on person2

Through polymorphism, marry on the man calls the person's marry method. Whereas on the woman's class it calls the woman's marry method. In effect changing the woman's person title to Mrs. That's polymorphism, the priest need not know the difference between a man and a woman to call the marry method. The marry method called is not the same one for the man and the woman.

Access Control

An object, its members and its methods can be assigned some kind of programming access. C++ and Java allow for public, protected and private access controls. Be aware that the defaults are different in both languages.

  • public means that any class type can access the member/method/class
  • protected means that the defining class and any classes which inherit from the class can access the protected member
  • private means that the member is not accessible from other class types, even if they inherit it.

C++ adds friends. In essence a class can specify what friends (other class types) can have access to its privates.

Java adds the package access control. Only classes defined in the same package can access each other's members with a package access control.

Constructors & Destructors

When a class is instantiated, i.e. a variable of that class is created, it is possible to write a method that does something, such as connecting to a database. When a class variable is destroyed, it is also possible to write a method to do something. This is a neat way of making sure that:

  1. When an object is created, it is ready to be used. No additional steps need to be taken to initialise the object.
  2. When an object is destroyed, the object cleans up any resources it has taken up.

Abstract Objects

Abstract classes are classes where most, but not all of the implementation is there. An abstract class cannot be created directly, it can only be used via another class which inherits from the abstract class, and which implements the abstract bits.

For example, we could define the marry method in person as abstract. This means that a person object cannot be created, and that man and woman should both implement a marry method.

Interface

An interface has no code implementation. It simply gives a list of methods. Classes can implement an interface, and hence be passed around as instances of the interface.

Interfaces are a powerful way of laying down a set of rules without actually implementing them. They're a bit like abstract classes, where all the methods are abstract.

Pattern based programming design and interfaces go hand in hand. Using this technology we can expose a module of code using only interfaces. All implementation is hidden. Hence we can change an implementation at will without affecting other modules which only use the interfaces to access the functionality. That is essentially how, for example we could write code to access Oracle, Sybase,.... without needing to worry about which DBMS it actually is.

 

  Copyright © 2000 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.