Object-Oriented Programming (OOPs) in Python
Python Made Easy - (Article 11) Telugu Explanation
1. What is Object-Oriented Programming?
OOPs ante “Object-Oriented Programming”.
Idi manam program ni objects and classes form lo divide chesi organize cheyyadam.
Simple ga cheppali ante:
Instead of writing code randomly, we create blueprints (classes) and use them to build objects (real things).
1.Real-Life Example (Simple Understanding)
🪑 Think about a chair factory.
- Factory lo “Chair” ane design untundi (size, color, material…).
→ This design = Class - Aa design base chesukoni create chesina prathi chair = Object
So, Class = Blueprint / Design
Object = Real thing created from that design
2. Why OOP?
OOPs use cheyyalsina main reason:
- Code easy ga organize cheyyachu.
- Reuse cheyyachu (once class create chesaka, multiple times use cheyyachu).
- Errors reduce avutayi.
- Large projects easy ga maintain cheyyachu.
3. Class and Object in Python
Syntax of Class
class ClassName:
# properties (variables)
# methods (functions)Example:
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = colordef show_details(self):
print("Brand:", self.brand)
print("Color:", self.color)# Creating objects
car1 = Car("BMW", "Black")
car2 = Car("Audi", "White")# Calling methods
car1.show_details()
car2.show_details()
Output:
Brand: BMW
Color: Black
Brand: Audi
Color: White4. Explanation of the Example
- class Car: → Blueprint create chestunnam.
- init() → Special function called constructor, object create ayyaka automatically run avuthundi.
- self → Current object ni refer chestundi (like “this” in other languages).
- car1, car2 → Two objects created from the same class.
- show_details() → Class lo define chesina method (function inside class).
5. Constructor in Python
Constructor ante init() method.
Object create ayyaka automatically call avuthundi.
Manam initial values ivvadam kosam use chestam.
class Student:
def __init__(self, name, age):
self.name = name
self.age = ages1 = Student("Naresh", 25)
print(s1.name)
print(s1.age)Output:
Naresh
256. self Keyword
self ante current object reference.
Without self, Python ki teliyadu e variable / method ye object ki sambandham ani.
Get D Naresh’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
class Example:
def greet(self):
print("Hello from", self)Idi run chesthe self actual memory location chupistundi.
7. Types of Methods
- Instance Method — Use
self, access instance variables. - Class Method — Use
@classmethodandcls. - Static Method — Use
@staticmethod, noselforcls.
Example:
class Demo:
company = "Infosys"def __init__(self, name):
self.name = name def show(self): # Instance Method
print("Employee:", self.name) @classmethod
def get_company(cls): # Class Method
print("Company:", cls.company) @staticmethod
def greet(): # Static Method
print("Welcome to the Company!")d = Demo("Naresh")
d.show()
Demo.get_company()
Demo.greet()
Output:
Employee: Naresh
Company: Infosys
Welcome to the Company!8. Four Pillars of OOPs
OOPs lo 4 main pillars untayi
9. Inheritance Example
class Parent:
def show_parent(self):
print("I am the Parent")class Child(Parent):
def show_child(self):
print("I am the Child")obj = Child()
obj.show_parent()
obj.show_child()
Output:
I am the Parent
I am the ChildExplanation:
Child class → Parent class methods ni use chesindi.
10. Polymorphism Example
class Bird:
def sound(self):
print("Some generic bird sound")class Sparrow(Bird):
def sound(self):
print("Chirp Chirp")class Crow(Bird):
def sound(self):
print("Caw Caw")for bird in (Sparrow(), Crow()):
bird.sound()
Output:
Chirp Chirp
Caw Caw11. Encapsulation Example
class Account:
def __init__(self, balance):
self.__balance = balance # private variabledef get_balance(self):
return self.__balancea = Account(5000)
print(a.get_balance())
Output:
5000__balance private variable, direct ga access cheyyalem → encapsulation.
12. Abstraction Example
from abc import ABC, abstractmethodclass Shape(ABC):
@abstractmethod
def area(self):
passclass Circle(Shape):
def area(self):
print("Area of circle calculated here")obj = Circle()
obj.area()
Output:
Area of circle calculated hereAbstract class lo abstract methods untayi → compulsory override cheyyali.
13. Summary / Key Takeaways
- OOP → Object-Oriented Programming
- Class = Design, Object = Real-world entity
__init__()→ constructorself→ current object reference- 4 pillars → Encapsulation, Abstraction, Inheritance, Polymorphism
- Code easy ga reuse cheyyachu, organize cheyyachu
14. Practice Questions
1️.Create a class Student with name, age and marks.
- Method to display details.
2️.Create a BankAccount class with deposit and withdraw methods.
3️.Create a parent class Animal, child class Dog → method overriding practice.
4️.Create a class with @staticmethod and @classmethod examples.
Previous & Next
Next Article: 👉 12. File Handling in Python
Previous Article:👉 10. Strings & String Operations









