Object Oriented Programming — Thinking Abstractedly

Object oriented programming has to be one of the most powerful and scalable ways of developing. Object oriented programming (oop) is a programming methodology based on objects where these objects are organized into classes, which allow individual objects to be grouped together, according to techterms.

One of the main influencers of object oriented programming is Abstraction. It is the key to understanding inheritance which in turn is also a property of object oriented programming.

An easy example of abstraction in play is; given a situation where one has two classes: class car and class truck, both classes are indeed bound to have similar attributes when maybe a dealership is involved and both trucks and cars are up for sale. Instead of the tedious approach of coding both classes with attributes which would be defying what ‘DRY’ stands for… repetition, the best approach would be abstraction.

In this case, abstraction would involve creating a Vehicle class which would exhibit attributes of both cars and trucks since both objects are considered vehicles. A lot of code would still be repeated because we’d be having both car and truck classes this time extending class Vehicle. We’re not quite done yet, up to this point one is able to create objects via the Vehicle class which indeed easens up the work however, the instances to be created are intended to be either car or truck objects. What we have now are vehicle objects which is not specific on the type of vehicle being instantiated. Remember cars and trucks also may have different class attribute values like prices.

The issue adds up to class vehicle being an Abstract Base Class. Which means that Vehicle is only meant to be inherited from. Vehicle would have to exhibit an abstract method or virtual method like vehicle_category() which would deny instantiation of vehicle and in turn declare a vehicle category. The virtual method would also need to be implemented in the child classes in this case car and truck, declaring the category thus enabling instantiation of specific objects.

This would in turn reduce code repetition, enforce inheritance and abstraction in relation to object oriented programming. More details on object oriented programming are listed here.