Object Oriented Programming (OOP) Part 1
2024-06-23Let’s start with common challenges in programming:
- As a program or application grows larger, the codebase becomes more bloated, making code maintenance more complex and challenging.
- As more dependencies between code or functions across the application arise, issues of code duplication or repetition occur.
- The more features added to an application, the more prone the code is to errors during modifications or updates.
- As the system becomes more complex, the source code becomes difficult to understand, manage, and modify.
As developers encountering these issues, one possible solution might be… giving up??? Because the problem seems too complex to handle or too costly to fix. But of course not! Developers are not trained to give up but to solve problems.
From these issues arose a programming paradigm capable of addressing such challenges: Object-Oriented Programming (OOP).
OOP introduces modularity by creating objects for separate entities, making it easier to modify and maintain code. It introduces the concepts of inheritance and composition, enabling code reuse and reducing redundancy. OOP encapsulates data and methods related to a single “capsule” or unit, preventing accidental data modification.
So, how does OOP technically approach these problems?
Fundamentals of Object-Oriented Programming
The Object-Oriented Programming paradigm can be applied to almost all programming languages, especially high-level ones like Python, JavaScript, Java, Ruby, C#, C++, etc. However, there are a few languages that don’t support OOP, such as procedural programming languages and hardware assembly languages like C and FORTRAN.
Object-Oriented Programming is a programming paradigm that, as the name suggests, focuses on objects. This concept embodies the idea that everything around us is an object. Every object has characteristics represented by data (attributes) and has functions represented by methods.
Illustration of Class and Object Relationships
In Python, OOP aims to unify data and functions that work together into a single unit, so that no other part of the code can access that related data. This ensures data security and integrity.
Classes and Objects in OOP
To implement OOP in Python, we need a Class as a blueprint or prototype for the object we want to create. A class serves as a template that groups attributes (data) and methods (functions) the object will possess. It defines the structure and behavior the object will have.
On the other hand, an Object is a real-world manifestation of the characteristics defined by a class.
Analogy: Blueprint and Building
- Class the blueprint that contains the design, structure, and features of a building.
- Object a building constructed using the class blueprint.
Attributes
Attributes are variables associated with a Class. They store data linked to an object. There are two types of attributes: class attributes and instance attributes.
- Class attributes are shared across all instances of a class. These are defined within the class but outside any method.
Example of a class attribute:
class Dog:
species = "Canis lupus familiaris" # Class attribute
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age # Instance attribute
# Creating objects
dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 5)
# Accessing class attributes
print(Dog.species) # Outputs: Canis lupus familiaris
print(dog1.species) # Outputs: Canis lupus familiaris
print(dog2.species) # Outputs: Canis lupus familiaris
• Instance attributes are unique to each object. They are defined within the init method and accessed using the self keyword and dot notation.
Example of instance attributes:
class Dog:
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age # Instance attribute
# Creating objects with different instance attributes
dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 5)
# Accessing instance attributes
print(dog1.name) # Outputs: Buddy
print(dog2.age) # Outputs: 5
Methods
In a previous edition, we explained functions in Python and how to create them. In OOP, classes use the same concept of functions, but they are referred to as methods.
A method is a behavior or action that an object of a class can perform.
Here’s an analogy to differentiate between attributes and methods:
Example: Air Conditioner
• Attributes: Temperature setting, mode, timer setting. • Methods: Adjusting temperature, changing mode, turning the timer on/off.
There are several types of methods in OOP, such as instance methods, class methods, and static methods.
• Instance method: A method that runs when a class is instantiated. In the example below, display_info is an instance method because it runs and depends on class instantiation:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"{self.name} {self.age}")
• Class method: A method that depends on the class, not its instances. It uses the @classmethod decorator.
class Book:
total_books = 0
def __init__(self, title, author):
self.title = title
self.author = author
Book.total_books += 1
@classmethod
def get_total_books(cls):
return cls.total_books
• Static method: A method that doesn’t rely on class instantiation or the class itself. It uses the @staticmethod decorator.
class TemperatureConverter:
@staticmethod
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
The OOP approach simplifies the development of complex systems by creating interacting objects. This makes the codebase more organized and easier to maintain. Understanding the fundamentals of classes, objects, methods, and attributes is crucial for fully leveraging OOP in software development.
The beauty of programming is that there's always something new to learn. The learning process is never-ending, and that's what keeps it exciting. This is my ongoing journey into the world of programming. I'm constantly learning and growing, and I'm excited to share my experiences with you as I progress.
const developerName = "Ano Jumisa"