The Secrets You Wish You Knew Earlier

The Secrets You Wish You Knew Earlier

...the secret of being successful in a journey lies in knowing the rules guiding the paths

In computer programming, the above quote also comes to play; this is why I'm taking it upon myself to unveil this great secret wishing you won't tell another(smiles).

Programming is a term used to describe the process of writing instructions (codes). These codes are used to instruct the computer or build software for different purposes. Choosing a programming language could be quite tricky unless you understand the principles of these languages. This article is aimed at giving you the basic orientation of the most commonly used category of programming languages known as Object Oriented Programming(OOP). The popular languages you know or must have heard of, uses the principles of OOP in writing their instructions.

The Foundations of Object Oriented Programming(OOP)

OOP is one of the categories of programming. The languages in this category have one thing in common, which is representing data in object structure. Object structure means that each instance has two parameters: a property (attribute) and a method (function).

Too many technical terms right?

Objects

In OOP, Objects are the building blocks on which other operations stem from in your programming. It is self-contained, has properties and methods and can be assigned to a variable.

For example, you have a cat:

  • What are the characteristics of this cat?

You can say, its color, size, age, etc.

  • What can this cat do or what can be done to it?

In this answer, you'd think of the actions the cat can make or receive such as it meows, it sleeps, it can be fed, etc.

From the above example,

  • The Cat ⇒ Object
  • The Cat's characteristics ⇒ Properties(attributes)
  • The Cat's action ⇒ Methods(functions)

Some languages refer to properties as attributes and methods as functions. Either way, they're all referring to the same thing respectively. Methods are inbuilt functions but you can also create yours. This is beyond the scope of this article.

How to Create an Object

OOP languages have little difference in creating objects but each follows these steps:

  1. Object Declaration: You start by assigning a variable a name and data type of object.
  2. Object Instantiation: use the keyword peculiar to the language or other steps required to make an instance of your object.
  3. Object Initialization: Assign values to the object i.e properties and methods. Following these three steps, activates your object for use in your program.

Languages such as JavaScript have inbuilt methods for creating a new object at any instance; the method is object.create()

Classes

It is worthy to note that a collection of objects is known as a class. Let's go back to our first example where we listed a cat as an object. If John has a black cat, Jane has a brown cat and Jordan has a white cat, what do you do?

Declare an object for each cat!

Wow! You're correct but I'm sorry, trying this would take more time than necessary when the objects are in large numbers.

What then do you do?

Use a class!

A class is a prototype for creating objects. This means all I have to do is just create a general object with the overall properties and methods, then make reference to it when needed. It is declared with the class keyword.

You already know that a cat can have an owner, color, age etc. The owner, age and color become the properties of cats(remember the plural here). The methods (actions) also are stated when declaring the class and can be referenced at any point in your code(bearing the scope in mind)

Declaring classes in OOP Languages
Code Sample 1.0: Declaring a class in Python

class Cat: 
    def __init__(self, cat_color, cat_size, cat_owner,cat_age):
            self.color = cat_color
            self.size = cat_size
            self.owner = cat_owner
            self.age = cat_age
Code Sample 2.0: Declaring a class in JavaScript

class Cat {
  constructor(color, size, owner, age) {
    this.color = color;
    this.size = size;
    this.owner = owner;
    this.age = age;

  }
}

Principles of OOP

image.png

Inheritance

In simple terms, inheritance is the ability to take on the properties of another. Just like you got that beautiful wrist watch from your grandfather, objects behave similarly. OOP uses the principle of inheritance to make it easy for a class to use the properties and methods of another class. The class inheriting the properties and methods is known as the Child class while the class who's giving out its properties and methods for inheritance is called the Parent class.

image.png

A child class can have one or more parent classes in addition to its own class, this means having more than one inheritance. It can also have multiple objects.

Encapsulation

According to the dictionary, encapsulation means enclosing in form of a capsule. This implies that access to the content inside the capsule is highly restricted. In OOP, encapsulation isn't different from it's dictionary meaning; rather in this case, data and it's methods are bundled into one unit. It also hides access to some properties of the object. The most common way of implementing encapsulation in OOP is by the use of classes.

image.png

Polymorphism

Polymorphism in OOP is the ability of objects to take many forms due to inheritance. Remember, it was stated that a child class can have multiple parent classes. This implies that a particular object can also inherit the properties and methods of these multiple classes at any instance. Different methods can be called on this object while it gives a response based on its peculiar properties ( attributes). Using the earlier stated example; if I say, I saw Jane's cat. Then, you'd conclude that I saw a brown cat judging from the assigned attributes of Jane's cat. This is not the same as seeing John's cat but one thing they have in common is that they're all cats

Data Abstraction

Abstract simply means hidden. Data abstraction is a technique that refers to hiding some details(especially the internal implementation) of the data. After the initialization of the object or class as the case may be with properties and methods, all you need to do is call the method or property where and when needed. Data abstraction make you bother less about what's going on in the background and focus on the output.

image.png

So far, I have explained the basics of OOP and I'm hopefully that you have gained clarity on the subject matter but before we call it a wrap, let's take a peep into some programming languages that uses OOP techniques.

Examples of OOP Languages and their Applications

  • JavaScript: Web Development, Mobile Applications, Web Applications, Web servers etc.

  • Python: AI/ML, Web Development, Game Development, Data Science etc.

  • PHP: Back end Development, Data Representation, Web content management system, etc.

  • Java: Embedded systems, Game Application, Desktop GUI, Big Data Technology etc.

Choosing an OOP Language

The OOP languages have similar applications i.e. one language can be used to do the same application as another. Example, Python is used to develop web Applications just like JavaScript, same goes for PHP and Java that deals with data management respectively.

Although the basic nature of these languages may not perform too many functions, it's advisable to learn the basics(called vanilla in some languages) before moving on to learn a framework. For instance, python is used to develop web app using a framework called Django, JavaScript can be used in machine learning using Tensorflowjs.

What Next?

Select which of the applications of the OOP languages you'd want to develop, then pick a corresponding language to learn. Do not forget to weigh your options before settling for which.

Do you prefer writing mathematical operations in your codes or logical functions and variables?

The answer to the above question is what determines the next path in your learning journey.