Object Oriented Programming – What is it?

Object Oriented Programming- Introduction

In essence, object oriented programming (OOP) is a programming paradigm. A paradigm is a distinct set of concepts and methods that form a model or pattern. Furthermore, in computer science, a programming paradigm is a way of categorising programming languages based on their attributes.

Traditionally a computer program is a set of logical steps that accepts input, processes that input and produces an output. This is the case with functional and procedural programming, where the emphasis is on how the data is to be processed rather than the data itself. With object oriented programming, the focus is on the data itself rather than the logic needed to manipulate that data.

With OOP, a developer can define unique variables (data types) of data structures. Within these data structures a developer can define the functions or methods that can be applied specifically to it. The construct that encapsulates the set of variables and functions is known as a class. A class merely has a logical existence, and acts as a universal blueprint from which an instance can be created. An instance of a class is known as an object. An object will have a physical existence and is essentially a variable of the programmer defined class type which is used for actual functionality.

Object Oriented Programming- Characteristics

There are 4 core concepts behind object oriented programming. They are as follows:

Object Oriented Programming – Abstraction

In OOP, abstraction is the ability to portray data at a very high, conceptual level without any elaborate details. Objects only show what’s relevant to the use of other objects, while hiding the inessential implementation code. In other words, it is the process of exposing just the crucial aspects of an object, without worrying about what’s beneath. This greatly reduces the complexity of that entity for the users, and makes it simpler for developers to make changes and inclusions to it in future.

A sound real-life example of abstraction would be a coffee machine. A lot might be happening inside the unit, while it merely has a simple interface where a user can perform a small selection of functions. Similarly with a car, a driver can operate the car through a small selection of controls, without being all too concerned about what’s happening beneath the bonnet.

In short, with OOP, abstraction hides the irrelevant implementation code and only displays what the user is concerned with.

Object Oriented Programming – Encapsulation

Encapsulation is another one of the fundamental characteristics associated with object oriented programming. It describes how data and the methods that can be applied to that data are all enclosed within one unit.

Encapsulation can be implemented and demonstrated where objects keep their state private, inside a class. The other objects will not have direct access to this state, but rather, they can call a list of publicly declared functions / methods. An object will manage its own state through methods, and no other class can directly modify its state.

To give a very simple example: say we wanted to build a game – A game in which there are people and there is a dog. If we want to implement encapsulation we would encapsulate all ‘dog’ logic into a ‘Dog’ class. A basic version of that class might appear as follows:

Encapsulation Example

The state of the dog is defined the privately set variables; ‘name’, ‘hunger’ and ‘energy’. There is also a private method ‘bark’ declared. The Dog class can call the bark method whenever it wants but other classes (in the case of our game it would be a person class) cannot tell the dog when to bark. What a person can do is defined in the public methods ‘feed ( )’ and ‘sleep ( )’. Each of these public methods modifies the internal state of the dog in some way, and might even invoke the ‘bark ( )’ method.

Similarly, and perhaps in simpler terms – a person could feed the dog but cannot directly change how hungry the dog is. This is encapsulation.

Object Oriented Programming – Inheritance

In Object Oriented Programming, inheritance is another core characteristic which allows class types to take on the properties of an existing class. It is used to derive a class from an already existing class, which establishes a parent-child relationship. In the parent-child relationship, the parent can also be referred to as a superclass or base class, and the child which inherits from the superclass can be referred to as a subclass or derived class. The child class will inherit the visible properties and methods from the parent class and can also create additional methods and properties of its own.

To give a more real-world, non-technical example, subclasses and superclasses can be comprehended through an ‘is a’ relationship. A subclass is a more specific occurrence of superclass. In even simpler terms – a lemon is a citrus fruit, which is a fruit. The lemon class inherits all properties from the citrus fruit class which inherits from the fruit class. This is inheritance.

Object Oriented Programming – Polymorphism

The fourth and final core characteristic of Object Oriented Programming is Polymorphism. The word polymorphism comes from a Greek term meaning ‘many forms’. It is the ability for an instance of a class to exist in a variety of forms. It also describes how objects of different types can be instantiated from the same universal interface. Every unique object can provide its sole implementation of the interface.

In simple terms, polymorphism enables us to implement a class exactly like its parent in a way which there is no confusion with mixing types, but each subclass maintains its independent methods and properties as they are.

To give a real-world non-technical example of polymorphism we can imagine how a person at one time can have a variety of attributes. In other words a woman can at once be a mother, an employee, a wife and a sportsperson. This one instance of a person adopts different behaviour depending on what situation they are in. This is polymorphism.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: