Learn node js, angular, php best practices. A self learning platform to learn web development. Learn modern web development technologies.

July 21, 2019

Object Oriented Programming Principles You Must Know

oop principles



The core concept behind object oriented programming is to tie together data with operations that do things with it. Main intention of doing so is to make sure that no other part of the code can access this data except the function.


Why Object Oriented Programming?


Achieving Modularity: It enables us to write programs in a modular approach. A large program is divided into several smaller programs, each of them is known as a module. By using modular programming paradigm, it enables us to write large complex programs in more maintainable smaller chunks of programs.

Reducing Code Duplication With Inheritance: It reduces code duplication by using a concept known as inheritance. Suppose, we want to represent a Car and a Racing Car with object oriented programming. Both Car and Racing Car have some similarity between them, but Racing Car may have some extra features and functionalities that the normal Car don't have. By applying inheritance, we can easily represent these type of features without code duplication.

Achieving Flexibility With Polymorphism: Let's assume, we want to add drive functionality to both Car and Racing Car. Instead of creating driveCar() and driveRacingCar() functions, we just create a drive() function and we pass a parameter representing the type of car object (it can be car or racing car) to that function. So, depending on the type of car object, the drive() function will be called accordingly at runtime.

Building Blocks Of Object Oriented Programming


Class: It is the most primary block of object oriented programming. You can think of class as a way to represent user defined data type. Class acts as a glue that keeps data members and member functions together. Class represents the blueprint of something.

Let's imagine, in the Car class, there may be different cars with different name and brands. But all of them share some common features, like all of them have four wheels, speed limit, mileage range etc. So these are the car's properties or data members. Also, car can be driven by a driver, so drive is considered to be a functionality that a car must have.Class do not occupy any memory space, but object occupies space. So, drive will be considered as a member function of Car class.

Object: After bundling the data members and member functions together, we need to create an instance of that bundle. That instance is known as object. Creating object is necessary otherwise, we won't be able to carry out different operations on the car.

Let's understand this concept practically. Imagine a car factory. We create blue print of a specific car model, then we created multiple copies of it. So, blue print is the car class itself and we create multiple copies of car class based on the blue print provided.

Core Principles Of Object Oriented Programming


Encapsulation: Encapsulation refers to hiding the internal workings of an object from outside world. Generally, data members and member functions are wrapped up together as a single unit in encapsulation.

Let's assume, a car has parking sensors installed in it. To the outside world, it will alert the driver if the car is getting too close to an obstacle, but we are not aware about the detail inner workings of it. To the external world, there is only one switch that turns this feature on or off and rest of the complexity is hidden.

Inheritance: In simple words, inheritance refers to the capability of a class to acquire some or all of the properties of another class.

For example, if we consider two classes Car and Racing car, then both of them are cars. So, Racing car will have some features and functionalities same as the Car. To reflect this type of relationship of two classes, we can use inheritance.

Polymorphism: Poly means many, phism means forms. So, polymorphism means many forms. After applying polymorphism, the function that will be invoked is determined at runtime based on the type of the object.

Let's consider a real world example, a car have gear transmission system. It has four front gears and one backward gear. When the engine is accelerated then depending upon which gear is engaged different amount of power and movement is delivered to the car.

Implementing Object Oriented Programming in PHP


Creating Class

Object Oriented Programming Creating a Class


To represent all principles that we discussed above, we need to define which data members and member functions go together by placing them inside a class.


In the above code snippet, a Car class is defined, where 3 data members and 1 member function is defined. Note, the use of $this in drive() function, it refers to the particular instance of the object that's currently being manipulated. The protected keyword acts as an access modifier that allows the data member to be accessible from within the Car class, and all the classes that inherit the Car class.

Creating Object


After the data members and member functions are defined within the class. We need to create an object of the class to execute various operations on that object.


Object Oriented Programming Creating Object


In above code snippet, an object called brand_new_car is created and drive function is invoked on that object.


Creating Constructor


Suppose, we want to set a value to the object at the time of creation of it. Then we can do so using one of PHP's magic methods i.e, __construct.

Object Oriented Programmin Creating Constructor


So, we created a constructor that takes a single parameter called new_speed. Then we assigned the value to the car class's own data member (i.e, speed). Later we invoked the constructor by passing a value at the time of creating object of the car class.

Creating Static Data Members


If we want to store some data that pertains to the class rather than to any one instance of the class. Then we can use static keyword to do so.

object Oriented Programming Creating Static Data Member


Here, the car_bought keeps track of total number of cars bought across all car instances. This data member is a feature of the Car class itself, not any single Car object.

To access static data members, outside the class, the scope resolution operator is used with the name of the class.

Object Oriented Programming Accessing Static Data Member


To access it from within the class the scope resolution operator is prefixed with the keyword $self.

Object Oriented Programming Accessing Static Data Member Within Class


Creating Inheritance


Object Oriented Programming Creating Inheritance


In the above code snippet, a RacingCar class is created that inherits from the Car class. Then the constructor of the parent class (i.e, Car) is called to get the value from the parent class's constructor and pass that value to the child class as well. After that, an instance of racing car is created and a member function is called to show the extra feature that racing car already possesses. Here, Car is the parent class and RacingCar is a child or derived class.

Note: A class may be extended directly from only one class at a time in PHP.

Creating Interface

It allows a way to such that the member functions that child or derived classes must implement without providing an implementation. In other words, if we wanted the implementation will be done by the derived classes that will be responsible for implementing that interface, then we have to define an interface.

Object Oriented Programming Creating Interface


Implementing Interface


If you have decalred a Car interface, then in order to use the interface, a class must implement that interface. A class can implement the interface using the keyword implements.

Object Oriented Programming Implementing Interface


Note: A class can implement more than one interface at a time in PHP.

Object Oriented Programming Implementing Multiple Interfaces


In the above code snippet, the RacingCar class implements 2 interfaces (i.e, Car and SpeedyCar).

Creating Abstract Classes


Let's assume, you have a requirement where you want to provide part of an implementation for a class, but you do not want to allow instances of a class to be created until a more specific class extends it. To fulfill this type of requirement, you can use abstract class.

Object Oriented Programming Creating Abstract Class


In the above code snippet, Car class is defined as an abstract class that contains an abstract function called drive. To make use of this class, you have to create a class that extends this abstract class, and then you need to create instance of the child class and invoke the method using that child class object.

Creating Final Method and Final Class


If you do not want to allow a method to be overwritten in a child or derived class, you can declare that method as a final method. If you try to call the final method in child class, PHP interpreter will display an error message.

Object Oriented Programming Final Method Final Class


Final Words


If you find this post about object oriented princles helpful, don't forget to share this article among others. Thank you!

No comments:

Post a comment