Introduction to Object Oriented Programming

Object-oriented programming (OOP) is not a language unto itself, but rather a programming practice. OOP is seemingly easy at the surface, but can be quite challenging to master. In fact, many programmers coming from the procedural world of languages such as C or COBOL find they need to make a paradigm shift in how they think about programming. Even programmers who work with object-based languages such as VBA find the same paradigm shift inevitable. The paradigm shift I refer to is that of relating data, structures, and business requirements to objects.

OOP contains five core concepts, which are objects, classes, encapsulation, inheritance, and polymorphism:

• Objects represent a real-world thing such as person, place, or thing. Objects have behaviors and attributes.

• Classes are the blueprint for objects. They define how objects behave and how they expose attributes.

• Encapsulation hides implementation details from a user.

• Inheritance allows one class to inherit the features of another class.

• Polymorphism allows a class to implement the same operation in a number of different ways.

Unfortunately, VBA does not support inheritance or polymorphism in OOP's truest sense. Nevertheless, object-oriented programming in VBA allows the implementation of one of the most important benefits of OOP development known as encapsulation. In OOP terms, encapsulation allows programmers to reduce code complexity by hiding data and complex structures in classes. You and other programmers simply instantiate (create an object from a class) these classes as objects and access the object's methods and properties. Encapsulating implementation details is a wonderful benefit of OOP. Not only are complex details hidden, but code reuse is promoted. In VBA, OOP development is achieved through custom objects that are defined in class modules. Once built, custom objects don't necessarily add new functionality to your code. In fact, the same code you write in class modules could be written in event procedures, subprocedures, and function procedures. The purpose of using class modules is to provide encapsulation, code-reuse, and self-documenting code. Programmers using your custom objects work with them just as they would with other built-in VBA objects such as the ones found in the ADO library (Connection and Recordset objects).

Development with OOP generally requires more planning up front than in other programming paradigms. This design phase is crucial to OOP and your system's success.

At the very minimum, OOP design includes the following tasks:

• Identify and map objects to programming and business requirements.

• Identify the actions (methods) and attributes (properties) of each object. This action is commonly referred to as identifying the responsibilities of each object.

• Identify the relationships between objects.

• Determine the scope of objects and their methods and properties.

0 0

Post a comment