Print

The Commonness of Elements

Classes and Objects

Fundamental to the concept of object orientation is the idea of class. A class is an abstract blueprint of a group of objects which share the same characteristics. In the object oriented programming world, developers define classes which are then used to create objects. Classes describe objects by defining the properties and abilities those objects have to communicate and perform actions. It’s important to differentiate between classes and objects. Objects are examples and members of a given class. Objects have the characteristics of a given class, but are not a class themselves. A class is not an object, but a collection of characteristics which make up an object.

How Objects are Modeled as Actors

There are three types of characteristics to any class. The first are the properties of the class. I like to think of these as nouns and adjectives. A customer class would possess the properties of name, address, phone, contact, credit limit andcustomerID etc. On the other hand, a product class would have properties like name, cost, list price, size, color and productID etc.

The second characteristic are the methods of the class. Methods are verbs that answer the question ‘how?’ and represent the powers and abilities of the class. A customer class, for instance would have the capability to create an order for merchandise, change an order within limits, or have the ability to make payment for an order.

The third characteristic is that of listening for events. Events answer the question ‘when?’. For instance, at the time a legitimate customer makes an order, the production class would ‘hear’ it and respond by using its methods to check inventory and to issue a manufacturing ticket, or a shipping slip. It is not only possible but likely that these methods will result in communication with another object of another class (or even the same class) to set off another series of actions.

Encapsulizaion

Class structure allows the developer to encapsulate the characteristics of the class, so that it is not exposed to the programming environment, except when it is absolutely necessary. At the same time, the classes will possess the required properties, methods and events to make the business process effective.

Inheritance

The theme of class concepts is central to modeling the world. It is due to the ability of classes to inherit the characteristics of other classes. This is a common occurrence in object oriented programming because it allows the developer to build on what has come before and to maintain consistence across a project, or any number of projects. The classes from which trait are inherited is called the base class, or super class. The class doing the inheriting is called the derived class. In some languages (C++, for example) a class may inherit from multiple classes. All object-oriented languages allow for linear multiple inheritance where the inheritance comes in a straight line- for instance, a terrier inherits traits from a dog class, which in t turn inherits traits from a mammal class and the mammal class inherits traits from an animal class. However, the term multiple inheritance refers to non-linear multiple inheritance, where a dolphin, for example might inherit traits from the mammal class, as well as a fish class.

One could use the animal class to make a bird class. The characteristics for the animal class (move, make sounds, eat) would be inherited by the bird class. In addition, the bird class would also have some additional methods (fly, lay eggs) and properties (have feathers and wings). Hence, one could take the bird class and create an eagle class with additional methods (hunt for fish, shriek).

Lifetime and Visibility

Objects are able to do these things because they are programmed with self-contained routines which listen and act and have properties which can be changed and manipulated. The properties and events which are visible to the rest of the program are defined by the class interface. Those visible properties and methods are defined by the use of the ‘Public’ keyword. Those which are not accessible to the rest of the program are defined by the keyword ‘Private’. There are two other modifiers, ‘protected’, and ‘friend’. Protected scope means that those properties and methods so defined are available only to that class and all classes derived from it. Friend scope means that the properties and methods are available only to other classes in the same code module.

Normally, the lifetime of a property is limited to the life of the object. When the object is destroyed, so, too, are all of the properties of that object. Shared fields are variables that can be accessed by all instances of a given class (defined by the ‘shared’ keyword). Shared fields are useful when you have a situation where all the objects of a given class compete for a limited fixed amount of resources.

You can use the ‘shared’ keyword to make a method static, which enables your calling that method without instantiating an object of that class.

Overloading

Overloading is that construct which allows the same word, or method to be used in different contexts. It is the context, or signature of the method which allows the program to distinguish between the differences.

Time flies like the wind.

Fruit flieFebruary 11, 2008

rst.fields(“name”)

In both the instances, the context of the statement is acquired by context. The verbal example is a humorous example, but the first two words in the first sentence are subject and verb; in the second, they are adjective, and subject. Similarly, in the second example the contents of the parentheses are in index in the first case, but the name of a field in the second.

Polymorphism

Polymorphism is the property of object oriented languages which permits the same method to define different processes. Consider the animal class used earlier which has the method ‘move’ defined in the animal base class. Classes derived from that animal base class will also have the ‘move’ method which they inherit from the base class, ‘animal’. However it will be useful to more accurately define the ‘move’ method for each different animal class derived from animal. For instance the ‘move’ method for a kangaroo class would involve hopping, for a jaguar would involve running and that of a dolphin would involve swimming.

Using the principle of polymorphism, each of the derived classes could also have a ‘move’ method that would override the ‘move’ method of the base animal class. The advantage to this is that whenever you are dealing with any animal, you could use the same ‘move’ command and the object would respond appropriately.

Resources

Tutorials: Object Oriented Prog ramming

  • This link provides comprehensive list of Tutorials on Object Oriented Programming

Lesson: Object Oriented Programming

  • This link provides list of various topics related to Object Oriented Progamming.

Elementary knowledge on Object Oriented Programming

  • This link provides elementary knowledge on Object Oriented Programming.