Table of Contents
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure software programs. OOP concepts help in organizing code in a more intuitive and reusable manner. In this post, we will explore the fundamentals of OOP in Python, including classes, objects, inheritance, polymorphism, and encapsulation.
A class is a blueprint for creating objects (a particular data structure), while an object is an instance of a class.
1.Creating a Class
To create a class, use the class
keyword followed by the class name and a colon. The class can contain methods (functions) and attributes (variables).
Example:
python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
Creating an Object
To create an object, call the class as if it were a function.
Example:
python
person1 = Person("Alice", 30)
print(person1.greet()) # Output: Hello, my name is Alice and I am 30 years old.
2. The __init__
Method
The __init__
method is a special method called a constructor, which is automatically called when a new object is instantiated. It is used to initialize the object’s attributes.
Example:
python
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"Car brand: {self.brand}, Model: {self.model}"
car1 = Car("Toyota", "Corolla")
print(car1.display_info()) # Output: Car brand: Toyota, Model: Corolla
3. Inheritance
Inheritance allows one class to inherit attributes and methods from another class. The class that inherits is called the child class, and the class being inherited from is called the parent class.
Example:
python
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
return "Some generic sound"
class Dog(Animal):
def make_sound(self):
return "Woof!"
dog = Dog("Buddy")
print(dog.name) # Output: Buddy
print(dog.make_sound()) # Output: Woof!
4. Polymorphism
Polymorphism allows methods to be used interchangeably with different types of objects, providing a way to perform a single action in different forms.
Example:
python
class Cat(Animal):
def make_sound(self):
return "Meow!"
def animal_sound(animal):
print(animal.make_sound())
dog = Dog("Buddy")
cat = Cat("Whiskers")
animal_sound(dog) # Output: Woof!
animal_sound(cat) # Output: Meow!
5. Encapsulation
Encapsulation is the concept of wrapping data and methods within a single unit (class) and restricting access to some of the object’s components.
Example:
python
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
return self.__balance
def withdraw(self, amount):
if amount > self.__balance:
return "Insufficient funds"
else:
self.__balance -= amount
return self.__balance
account = BankAccount("Alice", 1000)
print(account.deposit(500)) # Output: 1500
print(account.withdraw(200)) # Output: 1300
print(account.withdraw(2000)) # Output: Insufficient funds
6. Method Overriding
Method overriding allows a child class to provide a specific implementation of a method already defined in its parent class.
Example:
python
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
rect = Rectangle(4, 5)
print(rect.area()) # Output: 20
7. The super()
Function
The super()
function allows you to call methods from the parent class in a child class.
Example:
python
class Parent:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, I am {self.name}"
class Child(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age
def greet(self):
return f"Hello, I am {self.name} and I am {self.age} years old."
child = Child("Bob", 12)
print(child.greet()) # Output: Hello, I am Bob and I am 12 years old.
Conclusion
In this post, we covered the fundamentals of object-oriented programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and method overriding. Understanding these concepts is crucial for writing organized, reusable, and maintainable code. In the next post, we will explore advanced topics in Python, such as decorators, generators, and context managers. Stay tuned!

Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268
Most Commented