Object-Oriented Programming (OPP) concept in a simple way

Object-oriented programming ‘OOP’ is a fundamental programming paradigm that relies on the concept of Classes and Objects, used by almost every developer at some point in their development career. OOP is considered the most popular programming paradigm and known as the standard way of coding, taught by most IT-related educational institutes.  OOP helps us to write cleaner, reusable, scalable, modular, and maintainable code.

The concept of OOP in each programming language is almost the same with a little difference in syntax. We will be using PHP in this article but you can use any language as you wish.

The following are the basic and important building blocks of Object-oriented Programming (OOP).

1.     Polymorphism
2.     Inheritance
3.     Abstraction
4.     Encapsulation

Polymorphism
Polymorphism is a Greek word and means "many forms" a naming convention that helps to write code which is much more easy and manageable to use for the developers. There is a simple logic behind this word Polymorphism) that is: Functions in separate classes in an application/software that perform similar actions should have the same name.

In real life, if we have a look at the vehicle model we will be able to understand this. If we have to make a model for the vehicle then we can have many different shapes of vehicles like car, bicycle, tractor, van, etc. For each kind of vehicle, we have to write different classes. For the sake of simplicity let’s suppose that we want to print the number of wheels of each type. In this case, the Polymorphism principle says that methods/functions name that use to print the number of wheels should be the same regardless of the type of vehicle.

Let assume we call that method countWheels() which will be kept in an abstract class called Vehicle. For this let's make an interface for the vehicle as below:

Interface Vehicle {

   public function countWheels(){

   }

}

Now our concrete classes will be inheriting from this Vehicle (base) class.

Lets create a class for Car, Bicycle, Truck. We are not doing any complex calculation/operation for the sake of simplicity. Simple classes are as below:

class Car implements Vehicle() {
   private $wheels;
    __constructor($wheelQty){
         return $this->wheels = $wheelQty;
       }  

 public function countWheels(){
   echo “Car has”. $this->wheels. “wheels”;
  }

}

 

class Truck implements Vehicle(){

            private $wheels;

            __constructor($wheelQty){

        return $this->wheels = $wheelQty;

}

public function countWheels(){
  echo “Truck has”. $this->wheels. “wheels”;

  }

}

 

 

class Cycle implements Vehicle(){

            private $wheels;

            __constructor($wheelQty){

return $this->wheels = $wheelQty;

}

 

public function countWheels(){

echo “Cycle has”. $this->wheels. “wheels”;

}

}

 

Now let’s create objects for the above classes.

1.      $myCar = new Car(4);

2.     $myCycle = new Cycle(2);

3.     $myTruck = new Truck(8);

 

To print the number of wheels in $myCar, $myCycle, and $myTruck we simply call the countWheels() function which is common in all the class.

echo $myCar-> countWheels();

outlet will be : Car has  4 wheels;

 

echo $ myCycle-> countWheels();

outlet will be : Cycle has  2 wheels;

echo $ myTruck-> countWheels();

outlet will be : Trcuk has  8 wheels;

 

Inheritance

Inheritance is a relationship between two (Parent/child) class, where the child class inherits some or all the features of the base (parent class).

Let’s assume we have classes for Human and Teacher.

Class Human {

     public function eat($human){
          echo $human. “ is eating”;
   }

public function run($human){

         echo $human. “ is running”;

   }

}

Here we have created two methods inside of human class. Now let’s create a class for the teacher.

Class Teacher extends Human{

            public function teach($teacher){

            echo $teacher .“ is Teaching in a school”;

   }

}

We can see that we didn’t make any method as run and eat in the Teacher class instead we just extended this with Human class where we already have these two methods available. We can easily use these methods. We are creating an object of the Teacher class.

$ahmed = new Teacher();

echo  $ahmed->run(‘Ahmed’);

echo  $ahmed->eat(‘Ahmed’); 

We saw that inheritance help us to re-use the existing code very efficiently. In complex and large applications we can avoid code duplication by using Inheritance. We are always allowed to write whatever other methods in the child class, as we have one in the given example the teach() method is specific to the Teacher so we put this inside Teacher class instead of Human. If we have other classes like Driver, Student, Mother, Son, Actor, etc. than we put all the common method inside of our Human class and extends these class with this. All the Human methods will be available to each class.

 

ABSTRACTION

Abstraction is the concept of object-oriented programming that shows only essential attributes and "hides" unnecessary information. The main purpose of abstraction is to hide the unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts.

To show all the essential and hide the unnecessary information is called Abstraction in Object-Oriented Programming. The goal of Abstraction is to hide the insufficient information from the user.  

In real life let’s assume the coffee machine to demonstrate the concept of Abstraction. If we have to make a cup of coffee, the thing we don’t need to know is how the coffee machine is working internally to brew a cup of coffee. You don’t need to know the ideal temperature of the water or the amount of ground coffee you need to use. All these details have been taken care of and made the coffee machine to use. Now as a user it’s not your concern to know the actual internal complexity of the coffee machine.

Abstract class can’t be instantiated directly as it has no implementation. We must provide the implementation in our concrete class. Look at the example below:

abstract class MyBaseClass {

    abstract function showData();

}

class Derived extends MyBaseClass {

    function showData() {

        echo "Derived class";

    }

}

        

$b1 = new Derived;

$b1->showData();

The output will be as below: “Derived class”

 

ENCAPSULATION

Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse. The wrapping up of data and methods into a single unit, which is called Class, is known as encapsulation. The benefit of encapsulating is that it performs the task inside without making you worry.

Suppose if your friends/guest comes to your house and ask for some money. You will not let them go to pick the money from the location where you have kept them but you will go and take the required no of money and give it to your friend/guest.

 

Another example if someone wants to know your name he/she can’t directly pick that up from your brain/mind. He/she must have to ask you to tell your name. Thus everything works through a proper channel in this way. Actually, in encapsulation, you are exposing an interface to get or manipulate the data in a way that is secure and desirable for you. The outside world doesn’t have access to your information and they can only through the exposed channel/method. Look into the following example to understand.

class Student {

    private $fullname;

        public function getFullName() {

        return $this->fullname;

    }

    public function setFullName($fullname) {

        $this->fullname = $fullname;  

    }

}

   

$student = new Student();

$student->setFullName('Saleem Jafar');

$student->getFullName();

In the above Student Class, we have only the setter and getter method to access the Student Name. This concept is called Encapsulation in Object-Oriented Programming.

 

Tipps to remember the Basic elements of OOP:

Polymorphism: We use the implements keyword while implementing the Polymorphism.
Encapsulation: We use setter and getter (Set and Get) in capsulation to read/write data.
Inheritance: The word extends is being used in Inheritance
Abstract: While defining Abstract class we use the keyword abstract before the class

oop php abstract polymorphism encapsulation object-oriented programing
A
@ 13/11/2020
© All right reserved 2026