The 4 Principles of Object Oriented Programming

2 min. read

Object Oriented Programming (OOP) is a type of programming where you define a data type of a data structure and the functions that can be applied to that data structure. In other words, it's a method of programming to design and architect your code base in the form of classes with different functions that allow other classes to access data between each other. Common coding languages that are OOP languages are Java, PHP, C# and C++. The four main principles of Object Oriented Programming are encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation

Encapsulation binds together the data and the functions that manipulate the data. Meaning that the fields in an object are private so you cannot access them but there are functions that allow you to manipulate and update the data for that particular object. Those barriers between the data of a class and the functions that manipulate the data are defined by whether or not a variable or function is private, protected, or public. A public function can be accessed anywhere, a protected function can only be access within the class itself and by inheriting or parent classes, or and a private function can only be accessed within the class. Here's an example of encapsulation:

The get and set methods allow other classes to manipulate those variables in the Shampoo class however you cannot directly call those variables in the class at all.

 class Shampoo {

	private brand;
	private size;

	__construct() {}

	public function getBrand() {
	
			return $this->brand;
	}

	public function getSize() {

			return $this->size;
	}

	
	public function setBrand($brand)  {

			$this->brand = $brand;
	}

	public function setSize($size) {
		$this->size = $size;
	}

}

 $shampoo = new Shampoo()
 $shampoo->setBrand($brand);
 $shampoo->setSize($size)

Abstraction

Abstraction is used as a tool used to declare what functions are used in a class that decides to implement this abstract class. It's supposed to create a sense of simplicity while leaving the other classes to actually do the implementation of those operations.

If you're going to implement an Abstract class, you have to call all of the methods that are associated with that abstract class.

interface Shampoo {

	private brand;
	public function getBrand();
	public function setBrand($brand);
}

public class SheaMoisture implements Shampoo {

	private brand;
	public getBrand() {
		return $this->brand;
	}

	public setBrand($brand){
			$this->brand = $brand;
	}
}

Inheritance

Inheritance is what allows an object to get all the methods from the class that it inherits, but it can also override the existing implementation of those methods as well as add additional methods as well. You're getting the best of both worlds.

class Shampoo {

	private brand;
	private size;

	public function getBrand() {
			return $this->brand;
	}

	public function getSize() {
			return $this->size;
	}

	
	public function setBrand($brand)  {
			$this->brand = $brand;
	}

	public function setSize($size) {
		$this->size = $size;
	}

}

class SheaMoisture extends Shampoo {
	
	//Method is overridden
	public getBrand() {
		return "Shea Moisture";
	}
}

Polymorphism

Our last principle of OOP is polymorphism. Polymorphism is a principle that allows you to call the same function of two different classes that implement the same functions.

interface Shampoo {
	public function getBrand();
}


class SheaMoisture implements Shampoo {
	public __construct(){}
	
	public getBrand() {
		return "Shea Moisture";
	}
}

class MauiMoisture implements Shampoo {

	public __construct(){ }
	public getBrand() {
			return "Maui Moisture"
	}
}

//Implementation

$shampoo1 = new SheaMoisture();
$shampoo2 = new MauiMoisture();

echo $shampoo1->getBrand();
echo $shampoo2->getBrand();

Even though getBrand() is implemented in different places, you're still able to get the right value associated with the object.

My #1 tip for looking at a new codebase is to look to see how different classes relate to one another. Is it encapsulation? Do multiple classes implement this one class? What are each of the classes that are calling this one method doing? Creating those maps for yourself will make it that much easier to tackle any coding assignment.