The 4 pillars of OOP are 1) Encapsulation 2) Abstraction 3) Inheritance & Polymorphism. Here are some basic explanation of each why to use and where to use.
Encapsulation: Is the idea to define methods & properties as public, private & protected. We don’t want to explore the data & information outside of the class.
Encapsulation એ Object-Oriented Programming System (OOPS) નો એક મુખ્ય આધારશીલ સિદ્ધાંત છે. આનો અર્થ છે કે Data અને Methods (ફંક્શન્સ) ને એકસાથે બાંધીને એક unit (class) બનાવવી.
Encapsulation ની મદદથી તમે ડેટાને સુરક્ષિત રાખી શકો છો અને તેને સીધો ઍક્સેસ થતો અટકાવી શકો છો. Data માટે private, protected, અને public access modifiers નો ઉપયોગ થાય છે.
Example in Real Life: Bank Account
ઘરે બતાવી શકાય તેવું એકદમ સરળ ઉદાહરણ: બેંક એકાઉન્ટ
બેંક એકાઉન્ટમાં શું બને છે?
- Data (Attributes):
- એકાઉન્ટ બેલન્સ
- એકાઉન્ટ નંબર
- Methods (Functions):
- પૈસા જમા કરવું (Deposit)
- પૈસા કાઢવા (Withdraw)
બેંકનો નિયમ હોય છે કે તમે તમારું બેલન્સ સીધું બદલાવી શકતા નથી; એટલે કે, તમે માત્ર Deposit અથવા Withdraw ફંક્શનનો ઉપયોગ કરીને જ બેલન્સ બદલી શકો છો.
<?php
class BankAccount {
private $accountNumber;
private $balance;
// Constructor to initialize account
public function __construct($accountNumber, $initialBalance) {
$this->accountNumber = $accountNumber;
$this->balance = $initialBalance;
}
// Public method to deposit money
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
echo "₹$amount deposited. New balance: ₹{$this->balance}\n";
} else {
echo "Invalid amount to deposit.\n";
}
}
// Public method to withdraw money
public function withdraw($amount) {
if ($amount > 0 && $amount <= $this->balance) {
$this->balance -= $amount;
echo "₹$amount withdrawn. Remaining balance: ₹{$this->balance}\n";
} else {
echo "Invalid or insufficient balance.\n";
}
}
// Public method to view balance
public function getBalance() {
return $this->balance;
}
}
// Create a bank account
$account = new BankAccount("123456789", 5000);
// Perform actions
$account->deposit(1500);
$account->withdraw(2000);
echo "Final balance: ₹" . $account->getBalance();
?>
- Private Variables (private $balance, $accountNumber): આ ડેટા ડાયરેક્ટ ઍક્સેસ નહીં કરી શકાય. તે જ માત્ર ક્લાસની અંદરના મેથડ દ્વારા હેન્ડલ થાય છે.
- Public Methods (deposit(), withdraw()): ડેટાને ચેન્જ કરવા માટે ફક્ત આ ફંક્શન્સનો ઉપયોગ થાય છે.
- Security: બેલન્સ કોઈએ સીધું જ બદલી ન શકે (જે Encapsulationનું મુખ્ય લક્ષ્ય છે). આ પ્રક્રિયા ડેટાને સુરક્ષિત બનાવે છે.
અનેકાદિ ઉદાહરણ:
- Car Driving System: Accelerator અને Break કઈ રીતે કામ કરે છે તે તમે બહારથી નથી જોઈ શકતા. એ Engines ની અંદર Encapsulated છે.
- Mobile Phone: Call કરવા માટે ફક્ત Interfaceનો ઉપયોગ કરો છો; Calls ની અંદર કેવી રીતે કનેક્ટ થાય છે તે તમને ખબર નથી.
આમ, Encapsulation તમને સંકલનને સરળ બનાવે છે અને Security પ્રદાન કરે છે.
Abstraction: Is the idea to hide the complexity in front of users. For example we can push a button on car and it starts but how the inner mechanism works is hidden from the end users. We just need to call a simple function.
For example we are hiding creating/deleting post from guest users in WordPress. But Admin user can do that functions. So this is called hiding data or functions. Another example is hiding finance data to HR people and hiding increment data to finance people. So when we create an object of Finance user type, they can simple use some functions/methods but how it actually written is hidden to them. This is called Abstractions.
Abstraction એ Object-Oriented Programming System (OOPS) નો એક મહત્વપૂર્ણ સિદ્ધાંત છે, જે માત્ર જરૂરી માહિતી (features) બતાવે છે અને બાકી બધું છુપાવી દે છે.
અર્થાત્, abstractionના માધ્યમથી તમે ઉપયોગકર્તાને ફક્ત ઉપયોગી વિગતો બતાવો છો અને બેકએન્ડની જટિલતાઓ છુપાવો છો. PHP માં abstraction હાંસલ કરવા માટે Abstract Classes અને Interfaces નો ઉપયોગ થાય છે.
Example in Real Life: ATM Machine
તમારે ATM મશીન મારફતે પૈસા ઉપાડવા છે.
- તમે શું જાણો છો?
- તમારું કાર્ડ નાખવું.
- પિન દાખલ કરવો.
- રકમ પસંદ કરવી.
- પૈસા મળવા.
- તમે શું નથી જાણતા?
- એ કે ATM મશીન બેકએન્ડમાં બેંકના સર્વર સાથે કેવી રીતે જોડાય છે.
- ટ્રાન્ઝેક્શન કેમ અને કેવી રીતે પ્રક્રિયા થાય છે.
ATM મશીન તમને ફક્ત જરૂરી interface આપે છે, પરંતુ બાકી તમામ જટિલતાઓને છુપાવે છે. આ એ જ છે Abstraction.
<?php
// Abstract class
abstract class Shape {
// Abstract method (No implementation here)
abstract public function calculateArea();
// Normal method
public function display() {
echo "This is a shape.\n";
}
}
// Circle class extends abstract class
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
// Implementation of abstract method
public function calculateArea() {
return pi() * pow($this->radius, 2);
}
}
// Rectangle class extends abstract class
class Rectangle extends Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
// Implementation of abstract method
public function calculateArea() {
return $this->width * $this->height;
}
}
// Create objects and use them
$circle = new Circle(5);
echo "Circle Area: " . $circle->calculateArea() . "\n";
$rectangle = new Rectangle(4, 7);
echo "Rectangle Area: " . $rectangle->calculateArea() . "\n";
?>
Abstract Class (Shape):
- અહીં
Shape
નામની ક્લાસ છે જે દર્શાવે છે કે દરેક શેપનું એરિયા કૅલ્ક્યુલેટ કરવું જોઈએ. - પરંતુ
calculateArea
ફંક્શન માટે અમુક સ્પષ્ટ એપ્લિમેન્ટેશન નથી; આ દરેક શેપ માટે અલગ હશે.
Circle અને Rectangle Class:
- આ બે શેપ અલગ રીતે એરિયા ગણતા હોય છે, પણ બંને
Shape
માંથી inherited થાય છે. - તેઓ
calculateArea
ફંક્શનને અલગ અલગ રીતે define કરે છે.
Abstract Method:
calculateArea()
માત્ર declaration માટે છે,implementation માટે નથી.- આ માત્ર જરૂરી સુવિધાઓ બતાવે છે અને બેકએન્ડ લોજિક છુપાવે છે.
અન્ય રિયલ લાઇફ ઉદાહરણ:
- Vehicles:
- બધાં વાહનોમાં accelerate(), brake() અને steer() હોય છે.
- પણ કાર, બાઇક, અને ટ્રકમાં તેનો અમલ અલગ રીતે થાય છે.
- Appliances:
- તમારે તમારા વોશિંગ મશીન પર ફક્ત બટન દબાવવું પડે છે.
- તે બેકએન્ડમાં કેમ કામ કરે છે તે જાણવાની જરૂર નથી.
Inheritance: Is the idea to extends some properties & methods from the parent class to child class.
Inheritance એ Object-Oriented Programming System (OOPS) નો એક મહત્વપૂર્ણ સિદ્ધાંત છે, જ્યાં એક ક્લાસ (child class) બીજા ક્લાસ (parent class) ની પ્રોપર્ટીઝ અને મેથડ્સને વારસામાં મેળવે છે.
આનો ઉપયોગ ડેટા અને લોજિકના પુન: ઉપયોગ (reusability) માટે થાય છે, અને નવો functionalities ઉમેરીને એક્સ્ટેન્ડ કરી શકાય છે.
Example in Real Life: Family Hierarchy
તમારા પિતાને અમુક વિશેષતા છે:
- જેમ કે કઈક વસ્તુઓ સારી રીતે મેનેજ કરવી.
- તમને આ ગુણો પારિવારિક રીતે મળ્યા છે, એટલે કે તમે “અધિકાર” (features) વારસામાં લીધા છે.
તમે આ ગુણોનો ઉપયોગ કરી શકો છો અને સાથે નવી ચોકસાઈ અથવા કૌશલ્ય (new functionality) ઉમેરવા માટેનું સ્વતંત્રતા છે.
OOPSમાં Inheritanceનો અર્થ છે કે Child Class (તમે) Parent Class (તમારા પિતા)ના બધા મહત્ત્વના ફીચર્સ પચાવી શકે છે.
<?php
// Parent class
class Vehicle {
protected $brand;
public function __construct($brand) {
$this->brand = $brand;
}
public function start() {
echo "The $this->brand vehicle is starting.\n";
}
public function stop() {
echo "The $this->brand vehicle is stopping.\n";
}
}
// Child class
class Car extends Vehicle {
private $seats;
public function __construct($brand, $seats) {
parent::__construct($brand); // Call parent constructor
$this->seats = $seats;
}
public function showDetails() {
echo "This is a $this->brand car with $this->seats seats.\n";
}
}
// Another child class
class Bike extends Vehicle {
private $type;
public function __construct($brand, $type) {
parent::__construct($brand); // Call parent constructor
$this->type = $type;
}
public function showDetails() {
echo "This is a $this->brand bike of type $this->type.\n";
}
}
// Using the classes
$car = new Car("Toyota", 5);
$car->start();
$car->showDetails();
$car->stop();
$bike = new Bike("Honda", "Sports");
$bike->start();
$bike->showDetails();
$bike->stop();
?>
Parent Class (Vehicle):
Vehicle
માં સામાન્ય ફંક્શન છે, જેમ કેstart()
અનેstop()
.- આ ફંક્શન્સ દરેક પ્રકારના વાહન માટે લાગુ પડશે.
Child Classes (Car અને Bike):
Car
અનેBike
ક્લાસેVehicle
ક્લાસમાંથી બધા ફીચર્સ વારસામાં લીધા છે.- તેમણે પોતાનાં વિશિષ્ટ ફીચર્સ ઉમેર્યા છે, જેમ કે:
- Car માટે seats.
- Bike માટે type (sports, cruiser, etc.).
Code Reusability:
start()
અનેstop()
ફંક્શન્સ ફરીથી ન લખી અને માત્ર વારસામાં લીધા છે.
parent::__construct()
નો ઉપયોગ:
- Parent classના constructor ને કોલ કરવા માટે parent:: નો ઉપયોગ થાય છે.
અન્ય રિયલ લાઇફ ઉદાહરણ:
- Animal Kingdom:
- Parent Class: Animal
- Methods: eat(), sleep().
- Child Classes: Dog, Cat.
- Dog માટે bark(), Cat માટે meow() ઉમેરવા.
- Parent Class: Animal
- Organization:
- Parent Class: Employee
- Properties: name, salary.
- Child Classes: Manager, Developer
- Manager માટે projectAssign(), Developer માટે code() ઉમેરવા.
- Parent Class: Employee
Polymorphism: An object may have multiple forms.
Polymorphism એ Object-Oriented Programming System (OOPS) નો એક સિદ્ધાંત છે, જેમાં એક જ નામના method વિવિધ પ્રકારની classes દ્વારા અલગ રીતે કાર્ય કરે છે.
આ શબ્દ બે ભાગોથી બનેલો છે:
- Poly = બહુધારી (many)
- Morph = સ્વરૂપ (forms)
Polymorphismનો અર્થ છે કે “એક જ વસ્તુ અલગ અલગ સ્વરૂપે વર્તે છે.”
Real Life Example: Animals Sound
અન્ય ઉદાહરણ:
- બધાં પ્રાણીઓ પાસે “sound()” નામનું function હોય છે.
- હાથી, કૂતરો, મરઘાં વગેરે દરેક પ્રાણી માટે આ ફંક્શન અલગ રીતે કામ કરે છે:
- હાથી “Trumpet” કરે છે.
- કૂતરો “Bark” કરે છે.
- મરઘાં “Cluck” કરે છે.
Polymorphismમાં, એક જsound()
method છે, પણ દરેક class માટે તે અલગ રીતે કામ કરે છે.
<?php
// Parent class
class Animal {
public function sound() {
echo "Some generic animal sound\n";
}
}
// Child class: Dog
class Dog extends Animal {
public function sound() {
echo "Dog barks: Woof! Woof!\n";
}
}
// Child class: Cat
class Cat extends Animal {
public function sound() {
echo "Cat meows: Meow! Meow!\n";
}
}
// Child class: Cow
class Cow extends Animal {
public function sound() {
echo "Cow moos: Moo! Moo!\n";
}
}
// Function to demonstrate polymorphism
function makeAnimalSound(Animal $animal) {
$animal->sound();
}
// Create objects
$dog = new Dog();
$cat = new Cat();
$cow = new Cow();
// Call function
makeAnimalSound($dog); // Dog barks
makeAnimalSound($cat); // Cat meows
makeAnimalSound($cow); // Cow moos
?>
Parent Class (Animal):
Animal
ક્લાસમાં એક સામાન્યsound()
method છે, જે બધાં પ્રાણીઓમાં હૉલ્ડ કરે છે.
Child Classes (Dog, Cat, Cow):
- દરેક classએ
sound()
method override કર્યું છે અને તે પોતાનાં મર્યાદિત અવાજો આપે છે:- Dog: Woof!
- Cat: Meow!
- Cow: Moo!
Function (makeAnimalSound):
- આ ફંક્શન parent class નો ટાઈપ એસ્ટેબ્લિશ કરે છે, અને runtime polymorphism અનુસાર child classના મિથોડને ઓટોમેટિક ખોળી લે છે.
Real Life Analogies:
- Shapes Drawing:
- Parent Class: Shape
- Child Classes: Circle, Rectangle, Triangle.
- Method:
draw()
. - Circle, Rectangle, અને Triangle દરેક પોતાની અલગ drawing style આપી શકે છે.
- Payment System:
- Parent Class: Payment
- Child Classes: CreditCardPayment, PayPalPayment, BankTransfer.
- Method:
processPayment()
- દરેક પેમેન્ટના રીતો અલગ હશે.
Polymorphism ના ફાયદા:
- Code Reusability: Parent classના ફંક્શન્સનો પુન: ઉપયોગ કરે છે.
- Flexibility: Runtime પર objectના પ્રકાર પ્રમાણે method ને કાર્ય કરવા દે છે.
- Maintainability: અલગ અલગ object માટે અલગ પ્રકારના operations handle કરી શકે છે.
Here I want to explore the idea of abstract class. abstract class is having abstract methods (without method body) , just blank method defined. That’s it. Why we need this? just because when developer extends the abstract methods, system forced him to define each methods defined in abstract class. So what is required, is must to define.
On same idea the interface is working. The difference between abstract class & interface is: Interface do not having properties (class variables). Only abstract methods. Interface can extends using implements keyword.
![](https://dilip-parmar.in/wp-content/uploads/2024/10/oop.gif)
More I will explain later. Specially the use cases. Check this how this is explained to non technical guy.