Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm based on objects, which contain data (attributes) and functions (methods).
Procedural vs OOP
- Procedural: Focuses on functions and procedures
- OOP: Focuses on objects and data
Advantages
- Code reusability
- Better organization
- Easier maintenance
- Scalability
- Classes and Objects
What is a Class
A class is a blueprint for creating objects.
class Student:
name = "Pooja"
What is an Object
An object is an instance of a class.
s1 = Student()
print(s1.name)
Instance Variables and Methods
class Student:
def __init__(self, name):
self.name = name def display(self):
print("Name:", self.name)s1 = Student("Pooja")
s1.display()
- Constructors
What is a Constructor
A constructor is a special method used to initialize objects.
inital Method
class Person:
def __init__(self, name):
self.name = name
Parameterized Constructor
p1 = Person("Pooja")
Default Constructor
class Test:
def __init__(self):
print("Constructor called")
- Inheritance
Inheritance allows one class to inherit properties and methods from another.
Example
class Animal:
def speak(self):
print("Animal speaks")class Dog(Animal):
def bark(self):
print("Dog barks")d = Dog()
d.speak()
d.bark()
Method Overriding
class Dog(Animal):
def speak(self):
print("Dog barks")
- Polymorphism
Polymorphism allows methods to behave differently based on the object.
Example
class Bird:
def sound(self):
print("Bird sound")class Sparrow(Bird):
def sound(self):
print("Chirp")class Crow(Bird):
def sound(self):
print("Caw")for bird in (Sparrow(), Crow()):
bird.sound()
Operator Overloading
print(2 + 3) # Addition
print("Hello" + " World") # Concatenation
11.6 Encapsulation
Encapsulation means wrapping data and methods together and restricting access.
Access Modifiers
- Public: Accessible everywhere
- Protected:
_variable - Private:
__variable
Example
class Bank:
def __init__(self):
self.__balance = 0 def deposit(self, amount):
self.__balance += amount def get_balance(self):
return self.__balanceb = Bank()
b.deposit(1000)
print(b.get_balance())
11.7 Abstraction
Abstraction hides implementation details and shows only essential features.
Using abc Module
from abc import ABC, abstractmethodclass Shape(ABC):
@abstractmethod
def area(self):
passclass Square(Shape):
def area(self):
return 4 * 4
Key Points
- Cannot create object of abstract class
- Must implement abstract methods in child class
Conclusion
Object-Oriented Programming is a powerful approach that helps in designing efficient and scalable applications. Concepts like classes, inheritance, polymorphism, encapsulation, and abstraction make code more organized and reusable.
By mastering OOP, learners can build real-world applications with better structure and maintainability. These concepts are widely used in software development, web frameworks, and large-scale systems.
Understanding OOP is a major step toward becoming an advanced Python developer and opens the door to professional software development.
Leave a Reply