Elliot Choules

An introduction to Object Oriented Programming

August 08, 2019

Object Oriented Programming (OOP) is a programming paradigm that focuses on objects that interact cleanly with one another.

An object is a representation of any entity that is used within an application. They possess properties and methods that are used to help fulfil its purpose. Objects are instances of a class (sometimes referred to as a business object), so you could look at classes as a sort of cookie cutter.

Using object oriented programming helps ensure that applications are flexible, testable and easily maintainable. The paradigm can be split into four pillars:

Abstraction

The process of abstraction is to identify classes for your application. It’s important to work out what members are needed for each class to fulfil its purpose,ignoring any extraneous details.

Encapsulation

This is a technique used to obscure/encapsulate data and its implementation within a class which in turn hides complexities. The idea is that no other code in the application should need to worry about these details. Any details that should be known to other classes can be exposed via getters and setters. By exposing these details we are defining the class interface to the rest of the application.

For example, we know a car has an engine. We don’t know how the engine works (the impementation), but we do know that if we push the accelerator we move forward.

Inheritance

Inheritance is where one object is inherited by another. This relationship between classes means that they inherit the properties of the parent class. Using inheritance results less duplicate code because it’ll be located in the base class rather than in each derived class which also results in improved maintainability.

Polymorphism

Polymorph is a greek word that means “many shapes”. The concept of polymorphism is that a method can behave differently depending on the type of object that calls it. For example, a base class could have a method defined inside and a class that derives from it could have an alternative implementation that overrides the base method, therefore having “multiple shapes”.


Elliot Choules

Developer and Umbraco Certified Professional.
Twitter