OOP Basics
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, which are instances of classes. This approach helps structure programs for better readability, reusability, and scalability.
Key OOP Concepts
- Class:
- A blueprint for creating objects.
- Defines attributes (data) and methods (functions).
- Object:
- An instance of a class.
- Contains specific data and can perform actions defined in the class.
- Attributes:
- Variables that store data for objects.
- Methods:
- Functions defined inside a class to perform actions.
Defining a Class
Use the class
keyword to define a class.
Example:
class Person:
def __init__(self, name, age):
self.name = name # Attribute
self.age = age # Attribute
def greet(self): # Method
print(f"Hello, my name is {self.name}.")
Creating an Object
Create an object by calling the class like a function.
Example:
# Create an object
person1 = Person("Alice", 30)
# Access attributes
print(person1.name) # Output: Alice
print(person1.age) # Output: 30
# Call methods
person1.greet() # Output: Hello, my name is Alice.
The __init__
Method
The __init__
method initializes object attributes when an object is created.
Example:
class Dog:
def __init__(self, breed, color):
self.breed = breed
self.color = color
dog1 = Dog("Labrador", "Yellow")
print(dog1.breed) # Output: Labrador
Adding Methods
Methods define the behavior of a class.
Example:
class Calculator:
def add(self, a, b):
return a + b
calc = Calculator()
print(calc.add(3, 5)) # Output: 8
Class vs Instance Attributes
- Class Attributes:
- Shared across all instances of the class.
- Defined outside the
__init__
method.
- Instance Attributes:
- Unique to each object.
- Defined inside the
__init__
method.
Example:
class Car:
wheels = 4 # Class attribute
def __init__(self, brand):
self.brand = brand # Instance attribute
car1 = Car("Toyota")
car2 = Car("Ford")
print(car1.wheels) # Output: 4
print(car1.brand) # Output: Toyota
Encapsulation
Encapsulation restricts access to certain parts of an object to protect its internal state.
- Use
_attribute
for protected attributes. - Use
__attribute
for private attributes.
Example:
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = BankAccount(100)
account.deposit(50)
print(account.get_balance()) # Output: 150
Benefits of OOP
- Modularity: Code is organized into classes and objects.
- Reusability: Classes can be reused in other programs.
- Scalability: Easy to manage and extend large programs.
- Data Hiding: Protects internal data from direct modification.
Practice Exercises
- Create a Class:
- Define a class
Student
with attributesname
andgrade
. - Add a method
is_passing
that returnsTrue
if the grade is >= 50.
- Define a class
- Bank Account:
- Create a class
Account
with methods todeposit
,withdraw
, andcheck_balance
.
- Create a class
- Car Class:
- Define a class
Car
with class attributewheels
and instance attributesbrand
andmodel
. - Add a method to display car details.
- Define a class
- Encapsulation:
- Create a class
Library
with a private attribute_books
and methods to add, remove, and list books.
- Create a class
OOP is a cornerstone of modern programming, enabling the creation of organized and reusable code.
Next Lesson: Inheritance and Polymorphism