Day 15 of #100DaysOfCode in Python: Unraveling Object-Oriented Programming
Congratulations on reaching Day 15! Today, we immerse ourselves in the cornerstone of modern programming — Object-Oriented Programming…
Congratulations on reaching Day 15! Today, we immerse ourselves in the cornerstone of modern programming — Object-Oriented Programming (OOP). In Python, OOP fosters code structure, reusability, and real-world modeling. Brace yourself for an enriching journey into the world of classes and objects!
What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm rooted in the concept that objects represent both data and operations that can be performed on that data. It allows programmers to structure their software as a collection of objects, each encapsulating state and behavior.
Core Concepts of OOP
Classes: Blueprint or template for creating objects (instances).
Objects: Instance of a class representing real-world entities.
Attributes: Data encapsulated within objects.
Methods: Functions defined within a class, representing the behaviors of objects.
Creating a Class
In Python, a class is defined using the class keyword. Here’s how you can create a basic class:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"This is a {self.make} {self.model}.")__init__: Special method called a constructor, used for initializing objects.self: Refers to the instance of the class and accesses its attributes and methods.
Creating an Object
You can create an object (instance) of a class by calling the class. Each object can have different values for its attributes:
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")Accessing Attributes and Methods
You can access an object’s attributes and methods using dot notation:
print(car1.make) # Outputs: Toyota
car2.display_info() # Outputs: This is a Honda Civic.Inheritance in OOP
Inheritance allows a class (child class) to inherit attributes and methods from another class (parent class), promoting code reusability.
class ElectricCar(Car):
def __init__(self, make, model, battery_size):
super().__init__(make, model)
self.battery_size = battery_size
def display_battery_info(self):
print(f"This car has a {self.battery_size}-kWh battery.")super(): Calls a method from the parent class.ElectricCar(Car): Indicates thatElectricCaris inheriting fromCar.
Polymorphism in OOP
Polymorphism allows different classes to be treated as instances of the same class through inheritance. It enhances flexibility and maintainability.
class GasCar(Car):
def refuel(self):
print("Refueling the car with gas.")
class ElectricCar(Car):
def refuel(self):
print("Charging the car’s battery.")car = GasCar("Ford", "F-150")
car.refuel() # Outputs: Refueling the car with gas.Challenges for Day 15
Create a Class: Create a class representing a Book with attributes like title, author, and pages, and methods to display information about the book.
Inheritance: Extend the Book class to create a class representing an eBook, adding attributes specific to electronic books.
Polymorphism: Create different classes representing different types of media (Book, Movie, Song) with a common method, like
display_info(), and demonstrate polymorphism.
Conclusion
Diving into OOP is like acquiring a new lens to view and solve programming problems. It offers a structured, clean, and efficient way of writing code by bundling data and behavior into objects. With each day, as you delve deeper into Python, a world brimming with endless possibilities and opportunities unveils itself. Keep coding and let the world of OOP unfold! 🌟🚀


