Introduction
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which encapsulate data and behavior. Python supports OOP through classes and objects, enabling modular and reusable code.
1. Defining Classes and Creating Objects
Creating a Class
class Person:
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
Creating an Object
person1 = Person("Alice", 25)
print(person1.greet())
2. Class Attributes and Instance Attributes
Instance Attributes (Unique to Each Object)
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")
print(car1.brand, car2.brand) # Output: Toyota Honda
Class Attributes (Shared Across All Objects)
class Employee:
company = "TechCorp" # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
emp1 = Employee("John")
print(emp1.company) # Output: TechCorp
3. Methods in Classes
Instance Methods (Operate on Object Instance)
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says Woof!"
dog1 = Dog("Buddy")
print(dog1.bark())
Class Methods (Operate on Class-Level Data)
class Animal:
species = "Mammal"
@classmethod
def get_species(cls):
return cls.species
print(Animal.get_species()) # Output: Mammal
Static Methods (Independent of Class & Instance)
class MathOperations:
@staticmethod
def add(a, b):
return a + b
print(MathOperations.add(3, 5)) # Output: 8
4. Inheritance (Extending Classes)
Basic Inheritance
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "I make a sound"
class Dog(Animal):
def speak(self):
return "Woof!"
dog = Dog("Buddy")
print(dog.speak()) # Output: Woof!
Overriding Methods
class Parent:
def show(self):
return "Parent Method"
class Child(Parent):
def show(self):
return "Child Method"
c = Child()
print(c.show()) # Output: Child Method
5. Encapsulation and Private Variables
Encapsulation restricts direct access to object data.
Private Attributes and Methods
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
return self.__balance
account = BankAccount(1000)
print(account.deposit(500)) # Output: 1500
# print(account.__balance) # This will raise an AttributeError
Exercises
- Create a
Student
class with attributesname
,grade
, and a method to print student details. - Implement a
Vehicle
class with aCar
subclass that overrides a method. - Create a
Calculator
class with a static method for multiplication. - Implement encapsulation by defining a private attribute in a class.
Conclusion
You have now learned Object-Oriented Programming (OOP) in Python, covering classes, objects, inheritance, encapsulation, and polymorphism. The next step is to learn about file handling and exception handling in Python.