The technique known as object-oriented programming, or OOP, is widely used to arrange code and thought processes. We may use it to create reusable, organized code portions known as “objects.”
With Python, this is straightforward to learn and super useful for many coding projects.
1. Classes and Objects
Consider a class as a template. It serves as a blueprint for an object’s appearance or functionality. For instance, you may have a Dog class. This class may identify a dog’s traits, including its name, age, and capacity for barking. Then, whenever you build a dog object (such a golden retriever or bulldog), you are generating an instance of the Dog class.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says woof!"
my_dog = Dog("Buddy", 5)
print(my_dog.bark()) # Output: Buddy says woof!
2. Attributes and Methods
Methods are the activities an object may do, whereas attributes are similar to variables attached to an object. name and age are attributes and bark is a method in the Dog example above.
A car example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def description(self):
return f"This car is a {self.brand} {self.model}."
my_car = Car("Toyota", "Corolla")
print(my_car.description()) # Output: This car is a Toyota Corolla.
3. The __init__ Method
This is a special method known as the initializer. Whenever you create a new object, __init__ sets up the initial values. So, whenever we make a Car, we can specify the brand and model right when the car is created.
4. Encapsulation
Encapsulation means keeping some information private. In Python, you can make an attribute “private” by adding an underscore (_) before it, or double underscores (__) for stricter privacy.
Example:
class BankAccount:
def __init__(self, balance=0):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = BankAccount()
account.deposit(100)
print(account.get_balance()) # Output: 100
5.Inheritance
Through inheritance, one class (like a dog) may receive traits from another class (like an animal).
This implies that it has all of Animal‘s qualities plus some additional of its own.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof!"
my_dog = Dog("Buddy")
print(my_dog.speak()) # Output: Woof!
6. Polymorphism
A single function termed polymorphic exhibits distinct behavior based on the object it is applied to. Speak might mean different things depending on whether we’re talking about a Dog or a Bird in our Animal example.
7. Abstraction
Abstraction is about hiding complex details and only showing the essentials. For this,Abstract classes are used in Python for this, and subclasses are needed to define specific methods.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius * self.radius
circle = Circle(5)
print(circle.area()) # Output: 78.53975
8. Composition
With composition, you can build a new class by combining objects from other classes instead of using inheritance.
class Engine:
def start(self):
return "Engine starts."
class Car:
def __init__(self):
self.engine = Engine()
def start(self):
return self.engine.start()
my_car = Car()
print(my_car.start()) # Output: Engine starts.