Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples.
Picture a cluttered garageđ ď¸. Tools are scattered everywhere, various shelves are overflowing with items and you have to move things around just to find a hammer. Of course, we can argue that itâs our garage and we know where everything is. But, other people will face problems when they use this garage to work!đ
We sometimes share our code with other programmers to work together. Unstructured and hard-to-read code may only work for one person, but not for two (or more) people. One way we can organize our code is to use object-oriented programming (OOP) concept. We organize code by grouping related properties and behaviors into logical units called classes. To take this even further, OOP is built with four pillars â Abstraction, Encapsulation, Inheritance and Polymorphism. These OOPs concepts allow us to create code that is organized, reusable, and easy to understand.
Letâs discuss these four pillars with their suitable examples in real-life đ.
Imagine driving a car đ. Car driver presses the accelerator to move. Driver doesnât need to know how the engine works. Abstraction shows only what is needed and hides implementation details.
// Abstract class
public abstract class Vehicle {
public abstract void accelerate(); // Focus on action, not details
}
// Concrete class
public class Car extends Vehicle {
@Override
public void accelerate() {
System.out.println("Car is accelerating!");
}
}
Encapsulations combine data (private variables) and methods (getter and setter) just like a medicine capsule đ has various medicines inside it. Letâs bank account balance. The account balance is private. We cannot change it directly. Instead, we can deposit/withdraw and check using secure setter and getter methods.
public class BankAccount {
private double bal; // đPrivate data
// Getter method
public double getBalance() {
return bal;
}
// Setter method with logic
public void depositBalance(double amt) {
if (amt > 0) {
bal += amt;
} else {
System.out.println("Insufficient balance!");
}
}
}
Inheritance allows a class (known as child class) can use properties of another class (known as parent class) and also can add its own unique properties. Inheritance avoids redundancy in code. For example, a child inherits some properties (like surname đ¨âđŚ) from their parents with its own unique properties.
// Parent class
class Father {
String surname = "Upadhyay";
void showSurname() { System.out.println("Surname: " + surname); }
}
// Child class
class Son extends Father {
void hobby() { System.out.println("Playing cricket!"); }
}
There are different types of inheritance in OOPs, like: Single, Multiple, Multilevel, Hierarchy and Hybrid Inheritance.
Polymorphism means many forms. Polymorphism allows us to use the same names for methods with different parameters and return types within the class. For example, we can define add function for adding two integers and two strings as below –
class Calculator {
// Add two integers
int addition(int x, int y) {
return x + y;
}
// Add two strings
String addition(String x, String y) {
return x + y;
}
}
We have two categories of polymorphism in OOPs:
- Compile-time (static) polymorphism:Â It is known as method overloading. It belongs to a single class.
- Run-time (dynamic) polymorphism:Â It is known as method overriding. It belongs to parent-child class relationship.
The four pillars of Object-Oriented ProgrammingâEncapsulation, Abstraction, Inheritance, and Polymorphismâwork together to make code safer, more organized, and easier to maintain.
- Abstraction focuses on whatâs needed and hides implementation details.
- Encapsulation locks up sensitive data in private spaces.
- Inheritance allows you to reuse and extend existing properties.
- Polymorphism allows you to use same name for multiple behaviors.
Just like managing a garage effectively keeps it functional and safe, applying OOP concepts in your programs are clean, extendable, and robust!đĄ
Question:Â Which OOP pillar limits direct access to object variables?
- A)Â Polymorphism
- B)Â Inheritance
- C)Â Encapsulation
- D)Â Abstraction
Option (C) Encapsulation is correct.
