OOP Basic Concepts

What is OOP? OOP stands for Object-oriented programming. It is a programming model organized around objects rather than actions or logic. Many languages have Object-oriented programming features such as Java, Javascript, C#, C++ and PHP. Although the syntax is different, most of the object-oriented programming languages share the same concepts.

So what is an object? In Computer Science, object can be a variable, data structure, function or a method that has some value and is referenced by an identifier. In class-based OOP, objects are instances of classes. Here’s an example class in PHP code:

<?php

class Produce {

	private $quantity;

	private $price;

	public function getFullPrice() {

		return $this->price * $this->quantity;

	}
}

?>
Simple Class declaration in PHP

One more example of a class declaration in Javascript:

class Produce {
	constructor(quantity, price) {
		this.quantity = quantity;
		this.price = price;
	}
	getFullPrice() {
		return this.quantity * this.price;
	}
}
Class declaration in Javascript

There are object properties and object methods. In previous examples, “quantity” and “price” are properties while “getFullPrice” is a method that returns the product of quantity and price. This is how an Object is instantiated in PHP:

<?php
// ....
// won't output anything as the properties don't have any value

$myObj = new Produce();
echo $myObj->getFullPrice();

?>
Object instantiation in PHP

A class can have a constructor. A constructor, also called “ctor”, is a special procedure called to create an object. It can accept arguments and put the values inside the object properties.

Encapsulation

Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Properties can be

  • public – no restrictions – the property is accessible to everyone/everything
  • protected – the property is accessible to the class itself and child classes
  • private – the property is accessible only in the scope of the class

Inheritance

Inheritance enables new objects to take on the properties of existing objects. A class that is used as the main (basis) is often called super, parent or base class. A class that inherits from a super class is called a derived class or a subclass. Usually, to extend a class, “extends” keyword is used during class declaration. Consider the following example in Javascript:

class Produce {
	constructor(quantity, price) {
		this.quantity = quantity;
		this.price = price;
	}
}

class Fruit extends Produce {
	// ....
}
Extending a super class in Javascript

Polymorphism

In OOP, Polymorphism is the object’s ability to appear in many different forms depending on their data type or class. It is the ability to redefine methods for derived classes.

 

For more information about Object-oriented programming, I suggest starting with this Wikipedia article.