Object-Oriented Programming (OOP) is a popular programming paradigm that is widely used in software development. It is based on the concept of objects, which are instances of classes that interact with each other. In this blog, we will discuss some of the basic OOP concepts with examples.
1. Objects
Objects are the fundamental building blocks of OOP. They are instances of classes and contain data and methods. A class is a blueprint or template that defines the properties and behaviors of an object. For example, consider the class "Car". A car can have properties like color, make, model, and year, and methods like start, stop, and accelerate.
class Car:
def __init__(self, color, make, model, year):
self.color = color
self.make = make
self.model = model
self.year = year
def start(self):
print("Starting the car.")
def stop(self):
print("Stopping the car.")
def accelerate(self):
print("Accelerating the car.")
2. Encapsulation
Encapsulation is a concept that involves bundling data and methods together within a class, such that the data is hidden from the outside world and can only be accessed through methods defined in the class. This is also known as data hiding. Encapsulation helps to keep the code organized, and it allows developers to change the internal workings of a class without affecting the rest of the code.
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount > self.__balance:
print("Insufficient balance.")
else:
self.__balance -= amount
def get_balance(self):
return self.__balance
In this example, the account_number and balance properties are encapsulated using double underscores. These properties can only be accessed through the methods deposit, withdraw, and get_balance.
3. Inheritance
Inheritance is a mechanism that allows a class to inherit properties and methods from another class. The class that inherits from another class is called a subclass, while the class that is inherited from is called a superclass. Inheritance allows developers to reuse code and reduces the amount of code that needs to be written.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
In this example, the Dog and Cat classes inherit from the Animal class. They both have a speak method, but they implement it differently.
4. Polymorphism
Polymorphism is the ability of an object to take on many forms. In OOP, polymorphism is achieved through method overloading and method overriding. Method overloading involves defining multiple methods with the same name in a class, but with different parameters. Method overriding involves redefining a method in a subclass that was already defined in the superclass.
class Calculator:
def add(self, x, y):
return x + y
def add(self, x, y, z):
return x + y + z
class ScientificCalculator(Calculator):
def sin(self, x):
return math.sin(x)
In this example, the Calculator class has two add methods,

Comments
Post a Comment