Object Oriented Programming

02 Jan 2020

The four major principles of Object Oriented Programming assist in the consolidation of an application’s functional and logical boundaries into units known as objects, which encourage atomicity, reuse and extensibility

Encapsulation

Encapsulation is the concept of creating a defined set of functional and logical members of an object.

When designing an object, typically the questions asked include

  1. “What is this object meant to represent?”
  2. “Which actor/user/capability is this object supporting?”
  3. “What information does this object convey?”
  4. “What behaviors does this object exhibit?”
  5. “How is this object different from other objects?”

Abstraction

Abstraction is the principle of expressing the intent of an object, through a minimal contract which consumers of the object interact.

When considering the abstraction requirements of an object it is common to ask

  1. “What is the minimum set of features which are needed to represent the information and behaviors of this object?”
  2. “Which features of this object are logically internal and/or logically external to the consumers of this object?”
  3. “What level of complexity is appropriate to understand the use of and the interaction with this object?”
  4. “What complexity can I hide from consumers of this object, and why is this a benefit to the consumers?”

There are benefits to abstraction which include

Inheritance

Inheritance is the principle of defining relationships between similar types with related purpose. The concepts of inheritance ensure that some features of base types are automatically accessible to derived types because they are made from the base type, but have additional behaviors and information which extend above the base type and its capabilities.

A trivial example of three levels of inheritance:

Questions asked when reviewing the inheritance design for objects include

  1. “Is this a general type or a specific type of an object?”
  2. “How is this object similar to other objects in this system?”
  3. “Is this information or behavior on this object needed by a more general or more specific object type?”
  4. “Can I get this behavior or information already from this object’s base type?”

There are other concepts which are worth considering also when reviewing inheritance

In general relationships between objects are typically

Polymorphism

Polymorphism is the principle of an objects behaviors and information being determined by the specific instance of the type.

A trivial example follows:

In the above example the behavior of the animal is determined by the type of animal, and the instance of the animal.

The benefits of Polymorphism include

Polymorphism is achieved through overloading and overriding