Object Oriented Programming in its simplest form
4 years ago, when I got newly introduced to the Object-Oriented Programming, I just couldn't get my head around it. The problem was I was relatively new to Programming in general, so I find it difficult to understand those terms that were used to explain the concept and to differentiate it from procedural programming. So, in this article I will try to my best to explain OOP concept in a very simplified way to even a beginner.
If you google the definition of Object-Oriented Programming, you will probably see definition like "OOP is a programming paradigm that uses objects – which are instances of classes – to design and organize code." or some other similar complex sentences which you might find very difficult to understand so let break it down.
The first 2 things you need to understand in OOP is object and class. So what is Object and class?
CLASS and OBJECT
Class is normally referred to as the template of creating an object, in simple words, an object is created from a class.
Let use an electric blender as an example.
You know how this works right. You can put solid pepper in it, and it returns blended pepper. You can put corn in it and it will return blended corn for you. It doesn't matter what you put in it, so far it can be blended it returns it blended type to you as the result. Very simple right. You don't need to really know how the blender works all you need is to put in it some substances (onions, pepper, fruits e.t.c) and it create the necessary substance for you. In this the blender is the class and whatever you put in the blender (pepper, onion, fruit e.t.c) are the attributes while the output (blended pepper, fruit juice e.t.c) is the object that was created from the class blender.
class BlendedProduct{
// Blended product attributes
String product;
int time;
// constructor that initializes the object
public BlendedProduct(String product, int time){
this.product = product;
this.time = time;
}
public void blend(){
System.err.println("Blending " + product + " in " + time+"minutes");
}
}
public class OOPJava {
public static void main(String[] args) {
// Creating individual blended product (objects) from the BlendedProduct class
BlendedProduct BlendTomatoes = new BlendedProduct("tomatoes", 2);
BlendedProduct BlendApples = new BlendedProduct("apple", 4);
// calling the blend methods for each product
BlendTomatoes.blend();
BlendApples.blend();
}
}
The above is the code illustration for our Blender sample. The blender class was created and it attributes were initialized. Then the methods that blends the product was also created inside the BlendedProduct class.
later on inside our OOPJava class we created 2 instances(objects) of the class BlendedProduct, then we call the blend method on both of the products.
I hope this illustration is easy enough for you to understand the very basic of OOP. Now there are four other concept of object oriented programming, which are inheritance, encapsulation, data abstraction and polymorphism. I would be discussing them in the next article. Thanks.