OOP & ColdFusion

Object-Oriented Programming is common term in programming language. It’s a vast concept but to sum it up in a single line, it is a set of concepts and techniques that make use of the “object” construct, to write more reusable, maintainable, and organized code. Objects are implemented differently in every language. In ColdFusion, we have ColdFusion Components (CFCs) that can be instantiated to create objects.
Anyone who has ever studied OOP must know that there are four main concepts, which are:
* Abstraction
* Encapsulation
* Inheritance
* Polymorphism
The main goal of abstraction is to handle complexities by hiding unnecessary details from user. Like when we drive a car, we are unaware of how the engine works. Abstraction is implemented in most languages by defining a class that has methods, properties & constructors.
In ColdFusion, class is simply a CFC. A CFC has private & public properties as well as methods. Variables defined in ‘this’ scope of CFC are public, while those in ‘variable’ scope are accessible to CFC. You can also define variables that are private to methods by defining them in ‘local’ scope. CFCs can be instantiated by either using CreateObject() or the new operator.

Encapsulation is the concept of keeping data & the methods operating on this data in a single unit, i.e. class. This concept also involves information hiding. The basic idea is to bundle accessor methods with data that are not visible from outside of a class. Accessor methods are the methods that give read/write access to private data, commonly known as getter/setter methods.
In ColdFusion, you can define accessors for CFC properties. ColdFusion can automatically generate accessors if you set the property ‘accessor’ as true for a CFC.

Inheritance is a way to reuse existing code, or a way to write code in one location that many objects can make use of. When an object of class B inherits from class A, object B contains all the code — that is, methods and data — from class A, plus all the code from object B.
In ColdFusion, CFCs can extend to inherit functions & properties. ColdFusion does not allow multiple inheritance but a CFC can implement multiple interfaces. Interfaces can be extended too.

Polymorphism describes the concept that objects of different types can be accessed through the same interface. In most languages, polymorphism is implemented by ‘Method Overloading‘ & ‘Method Overriding‘.

In ColdFusion, we support ‘Method Overriding’. This means that a child CFC can override parent CFC’s method to to customize or completely replace the behavior of that method.