
Object Oriented Programming Concepts Explained with Real-Life Examples
In this article, I am going to discuss object-oriented programming concepts with real-world examples. First of all, let’s see what is Object Oriented Programming.
What is OOP(Object Oriented Programming)
Object-Oriented Programming is a programming paradigm that structures code around objects (Objects, Real-Life Examples like: Tree, Car, Pen etc) that encapsulate data (variables) and behaviors(methods). It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.
The 4 core concepts of Object Oriented Programming are:
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Now, before understanding these 4 core concepts, let’s first understand what is class and an object.
Classes and Objects
Let’s say you are building an Avengers game. So, for building this game, you need to create different Avengers (Like Captain America, Iron-Man, Spider-Man, etc). Each avenger has properties and behavior.

So, In Programming, you need to store two sets of information: properties (data) and functionality (behavior)

So to implement this concept in programming, we use classes and objects. Class is like a blueprint from which you can make objects, and an object is an instance of a class.

Code Example
class Avenger {
String name;
int power;
void introTosSelf(String name) {
System.out.println("Hello I'm "+ name);
}
}
public class Main {
public static void main(String[] args) {
Avenger ironMan = new Avenger();
ironMan.name = "Iron-Man";
ironMan.power = 80;
ironMan.introTosSelf("Iron-Man");
}
}
class Avenger {
constructor(name, power) {
this.name = name;
this.power = power;
}
introToSelf() {
console.log("Hello I'm " + this.name);
}
}
const ironMan = new Avenger("Iron-Man", 80);
ironMan.introToSelf();
class Avenger:
def __init__(self, name, power):
self.name = name
self.power = power
def intro_to_self(self):
print("Hello I'm " + self.name)
iron_man = Avenger("Iron-Man", 80)
iron_man.intro_to_self()
#include
#include
class Avenger {
public:
std::string name;
int power;
void introToSelf(const std::string& name) {
std::cout << "Hello I'm " << name << std::endl;
}
};
int main() {
Avenger ironMan;
ironMan.name = "Iron-Man";
ironMan.power = 80;
ironMan.introToSelf("Iron-Man");
return 0;
}
Inheritance
Inheritance is a mechanism by which one class acquires the properties and behaviors of the parent class. In below example, New Captain America is Inheriting functionality and behaviors of young Captain America and also has its own functionality and behaviors.

Code Example
// ParentClass
class OldCaptainAmerica {
void introTosSelf() {
System.out.println("Hello I'm captain america");
}
}
// ChildClass
class NewCaptainAmerica extends OldCaptainAmerica {
void canFight() {
System.out.println("New captain america can fight");
}
}
public class Main {
public static void main(String[] args) {
NewCaptainAmerica newCaptainAmerica = new NewCaptainAmerica();
// Inherited method
newCaptainAmerica.introTosSelf();
// Child Class method
newCaptainAmerica.canFight();
}
}
// ParentClass
class OldCaptainAmerica {
introTosSelf() {
console.log("Hello I'm captain america");
}
}
// ChildClass
class NewCaptainAmerica extends OldCaptainAmerica {
canFight() {
console.log("New captain america can fight");
}
}
const newCaptainAmerica = new NewCaptainAmerica();
// Inherited method
newCaptainAmerica.introTosSelf();
// Child Class method
newCaptainAmerica.canFight();
class OldCaptainAmerica:
def intro_to_self(self):
print("Hello I'm captain america")
class NewCaptainAmerica(OldCaptainAmerica):
def can_fight(self):
print("New captain america can fight")
if __name__ == "__main__":
new_captain_america = NewCaptainAmerica()
# Inherited method
new_captain_america.intro_to_self()
# Child Class method
new_captain_america.can_fight()
#include
// ParentClass
class OldCaptainAmerica {
public:
void introTosSelf() {
std::cout << "Hello I'm captain america" << std::endl;
}
};
// ChildClass
class NewCaptainAmerica : public OldCaptainAmerica {
public:
void canFight() {
std::cout << "New captain america can fight" << std::endl;
}
};
int main() {
NewCaptainAmerica newCaptainAmerica;
// Inherited method
newCaptainAmerica.introTosSelf();
// Child Class method
newCaptainAmerica.canFight();
return 0;
}
Polymorphism
Polymorphism is the ability to exist in many forms. Like Spider-Man has multiple forms.

In Programming, there are mainly two types of polymorphism:
- Method Overloading
- Method Overriding
Method Overloading
In programming, method overloading is also known as compile-time polymorphism. In this type of polymorphism, more than one method in the same class shares the same name with different parameters.
Code Example
class SpiderMan {
void printName() {
System.out.println("Hello I'm Spiderman");
}
//Same method name with different parameter in same class
void printName(String name) {
System.out.println("Hello I'm " + name);
}
}
public class Main {
public static void main(String[] args) {
SpiderMan spiderMan = new SpiderMan();
spiderMan.printName();
spiderMan.printName("Ben Reilly");
}
}
class SpiderMan {
printName() {
console.log("Hello I'm Spiderman");
}
//Same method name with different parameter in same class
printName(name) {
console.log("Hello I'm " + name);
}
}
class Main {
static main(args) {
const spiderMan = new SpiderMan();
spiderMan.printName();
spiderMan.printName("Ben Reilly");
}
}
// Execute the main method
Main.main([]);
def product(a, b):
p = a * b
print(p)
def product(a, b, c):
p = a * b*c
print(p)
product(4, 5, 5)
#include
#include
class SpiderMan {
public:
void printName() {
std::cout << "Hello I'm Spiderman" << std::endl;
}
//Same method name with different parameter in same class
void printName(std::string name) {
std::cout << "Hello I'm " + name << std::endl;
}
};
int main() {
SpiderMan spiderMan;
spiderMan.printName();
spiderMan.printName("Ben Reilly");
return 0;
}
Method Overriding
In programming, method overriding is also known as run-time polymorphism. In this type of polymorphism, we have the same method name and parameters both in the child and parent classes, but have different implementations in the child class and in parent class.
Code Example
class SpiderMan {
void printName() {
System.out.println("Hello I'm Spiderman");
}
//Same method name with different parameter in same class
void printName(String name) {
System.out.println("Hello I'm " + name);
}
void canAttack() {
System.out.println("Spiderman can attack");
}
}
class SpiderManBenReilly extends SpiderMan {
@Override
void canAttack() {
System.out.println("Spiderman ben reilly can attack too");
}
}
public class Main {
public static void main(String[] args) {
SpiderManBenReilly spiderManBenReilly = new SpiderManBenReilly();
spiderManBenReilly.printName("Ben Reilly");
spiderManBenReilly.canAttack();
}
}
class SpiderMan {
printName(name) {
// Method overloading simulation - check number of arguments
if (arguments.length === 0) {
console.log("Hello I'm Spiderman");
} else {
//Same method name with different parameter in same class
console.log("Hello I'm " + name);
}
}
canAttack() {
console.log("Spiderman can attack");
}
}
class SpiderManBenReilly extends SpiderMan {
// @Override equivalent in JavaScript
canAttack() {
console.log("Spiderman ben reilly can attack too");
}
}
class Main {
static main() {
const spiderManBenReilly = new SpiderManBenReilly();
spiderManBenReilly.printName("Ben Reilly");
spiderManBenReilly.canAttack();
}
}
// Execute the main method
Main.main();
class SpiderMan:
def printName(self, name=None):
if name is None:
print("Hello I'm Spiderman")
else:
print("Hello I'm " + name)
def canAttack(self):
print("Spiderman can attack")
class SpiderManBenReilly(SpiderMan):
# Override
def canAttack(self):
print("Spiderman ben reilly can attack too")
if __name__ == "__main__":
spiderManBenReilly = SpiderManBenReilly()
spiderManBenReilly.printName("Ben Reilly")
spiderManBenReilly.canAttack()
#include
#include
class SpiderMan {
public:
void printName() {
std::cout << "Hello I'm Spiderman" << std::endl;
}
//Same method name with different parameter in same class
void printName(const std::string& name) {
std::cout << "Hello I'm " << name << std::endl;
}
virtual void canAttack() {
std::cout << "Spiderman can attack" << std::endl;
}
};
class SpiderManBenReilly : public SpiderMan {
public:
void canAttack() override {
std::cout << "Spiderman ben reilly can attack too" << std::endl;
}
};
int main() {
SpiderManBenReilly spiderManBenReilly;
spiderManBenReilly.printName("Ben Reilly");
spiderManBenReilly.canAttack();
return 0;
}
Abstraction
Abstraction is the concept of hiding the internal details and describing things in simple terms.

As you can see in the above example, Spider-Man hides his identity from the world by wearing a mask. Same as in programming, to hide complex implementation details of an object, we use abstraction.
It helps reduce complexity and allows the programmer to focus on what an object does, instead of how it does it.
Code Example
abstract class SpiderMan {
abstract void wearMsk();
}
class SpiderManBenReilly extends SpiderMan {
@Override
void wearMsk() {
System.out.println("Spider-Man wearing mask");
}
}
public class Main {
public static void main(String[] args) {
SpiderManBenReilly spiderManBenReilly = new SpiderManBenReilly();
spiderManBenReilly.wearMsk();
}
}
// Abstract class SpiderMan
class SpiderMan {
wearMsk() {
throw new Error("Abstract method 'wearMsk()' must be implemented");
}
}
class SpiderManBenReilly extends SpiderMan {
wearMsk() {
console.log("Spider-Man wearing mask");
}
}
class Main {
static main(args) {
const spiderManBenReilly = new SpiderManBenReilly();
spiderManBenReilly.wearMsk();
}
}
// Execute the main method
Main.main([]);
class SpiderMan:
def wear_msk(self):
raise NotImplementedError("Subclass must implement abstract method")
class SpiderManBenReilly(SpiderMan):
def wear_msk(self):
print("Spider-Man wearing mask")
if __name__ == "__main__":
spider_man_ben_reilly = SpiderManBenReilly()
spider_man_ben_reilly.wear_msk()
#include
class SpiderMan {
public:
virtual void wearMsk() = 0;
};
class SpiderManBenReilly : public SpiderMan {
public:
void wearMsk() override {
std::cout << "Spider-Man wearing mask" << std::endl;
}
};
int main() {
SpiderManBenReilly spiderManBenReilly;
spiderManBenReilly.wearMsk();
return 0;
}
Encapsulation
Encapsulation is a concept of combining of methods (behaviour) and variables (data) into a single entity called a class and restricting direct access to some of the object’s components.Same as in the below example, every avenger is included in Avengers Endgame.

It is one of the core principles of OOP that helps protect an object’s internal state and only allows controlled access.
Code Example
class Avenger {
private int power;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
}
public class Main {
public static void main(String[] args) {
Avenger ironMan = new Avenger();
ironMan.setName("Iron-Man");
ironMan.setPower(80);
System.out.println(ironMan.getName());
System.out.println(ironMan.getPower());
}
}
class Avenger {
constructor() {
this.power = 0;
this.name = "";
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
getPower() {
return this.power;
}
setPower(power) {
this.power = power;
}
}
function main() {
const ironMan = new Avenger();
ironMan.setName("Iron-Man");
ironMan.setPower(80);
console.log(ironMan.getName());
console.log(ironMan.getPower());
}
main();
class Avenger:
def __init__(self, name=None, power=None):
self.__name = name
self.__power = power
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
self.__name = name
@property
def power(self):
return self.__power
@power.setter
def power(self, power):
if isinstance(power, int):
self.__power = power
else:
raise TypeError("Power must be an integer.")
def main():
iron_man = Avenger()
iron_man.name = "Iron-Man"
iron_man.power = 80
print(iron_man.name)
print(iron_man.power)
if __name__ == "__main__":
main()
#include
#include
class Avenger {
private:
int power;
std::string name;
public:
// Getter for name
std::string getName() {
return name;
}
// Setter for name
void setName(std::string name) {
this->name = name;
}
// Getter for power
int getPower() {
return power;
}
// Setter for power
void setPower(int power) {
this->power = power;
}
};
int main() {
Avenger ironMan;
ironMan.setName("Iron-Man");
ironMan.setPower(80);
std::cout << ironMan.getName() << std::endl;
std::cout << ironMan.getPower() << std::endl;
return 0;
}
Why OOP Matters
Scalable and clean code
Reusable and modular
Secure and organized
Easy to debug and maintain
🧠 Final Thoughts
Object-Oriented Programming is not just about theory — it’s about thinking in objects and interactions, just like we do in real life.
If you found this guide useful:
✅ Bookmark it
✅ Share it with a friend
✅ Drop a comment or question below
Which one do you uses the most? Or which one confuses you the most?
Drop a comment below and let’s discuss!
Related Posts
Stay Connected
Follow me on Instagram for Java tips, coding reels, and much more.
Subscribe to my YouTube Channel for learning the art of programming.