Overview

Object-Oriented Programming (OOP) is a powerful paradigm that revolutionized the way we approach software development. It allows us to model real-world entities and their interactions in a structured and intuitive manner. In this article, we'll delve into the fundamental concepts of OOP—Classes, Objects, and Constructors—using a simple yet delightful example: a Coffee class.

Concept of Object Oriented Programming

Creating Your First Class

Let’s begin by crafting a simple class named Coffee, which starts as an empty blueprint, devoid of any content. Picture it as a blank canvas, ready to be filled.

Here's how the initial code structure would appear:

class Coffee:
    pass
Concept of Object Oriented Programming

Our next goal is to create an instance named drink using the Coffee class. Imagine that we are creating an object using the Coffee class blueprint. To achieve this, we can follow these steps:

Here's how the initial code structure would appear:

class Coffee:
    pass

drink = Coffee()
print(drink)
Concept of Object Oriented Programming

Now, let's discuss the expected output. When you run the code, you'll see an output that might seem a bit confusing at first:

OUTPUT:

While this output might appear messy initially, it actually provides valuable information. Here's what it means:

  • <__main__.Coffee object: This part indicates that an object of the class Coffee has been created in the current module (__main__).
  • at 0x00000210E1999120>: This portion represents the memory location of the object in hexadecimal notation. It's a unique identifier that helps Python manage and differentiate objects in memory.
Concept of Object Oriented Programming

In essence, this output tells us that we've successfully created an instance of the Coffee class and stored it in the variable drink. While the output format might not be immediately intuitive, it's a fundamental aspect of how Python represents objects and their memory locations.

As you continue to work with classes and objects, you'll become more familiar with this kind of output and its significance. It's a behind-the-scenes look at how Python manages the objects you create in your code.

Class Instantiation

In the previous section, we learned how to create an instance named drink using the Coffee class. This process is known as class instantiation, where we bring the blueprint to life by crafting an actual object. The exciting part is that we can generate as many objects as we desire, each with its unique set of properties and characteristics

Let's explore this further by creating two more instances: drink1 and drink2.

Here's how the initial code structure would appear:

class Coffee:
    pass

drink1 = Coffee()
drink2 = Coffee()
Concept of Object Oriented Programming

You might have noticed that running the code produces no visible output. This is because we haven't instructed the program to display anything yet.

Adding Arbitrary Attributes

Arbitrary attributes refer to the ability to assign custom properties to objects. These properties are not predefined within the class blueprint; instead, we can create them based on our specific needs. Think of arbitrary attributes as labels we can attach to objects to describe unique features that aren't part of the original class definition. This flexibility allows us to tailor each instance to our requirements without altering the original class structure.

Imagine you have a class, just like our Coffee example:

class Coffee:
    pass

Now, let's say we've created an instance of the class called drink:

drink = Coffee()

At this point, the drink object doesn't have any properties or attributes beyond what the Coffee class blueprint defines. However, with arbitrary attributes, we can go beyond the class blueprint and add new properties that are unique to this specific instance. For instance, consider the lines of code below:

drink.name = "Latte"
drink.calories = 200

In these lines, we're attaching two arbitrary attributes to the drink object: name and calories. These attributes are not predefined within the Coffee class, but we can effortlessly introduce them to suit our needs.

To put it simply, arbitrary attributes act like labels we can stick onto objects to describe specific characteristics that weren't initially part of the original class design. In the example above, we're labeling the drink object with a name ("Latte") and a calorie count (200).

The beauty of this feature lies in its flexibility. It enables us to customize each instance of a class without altering the class blueprint itself. This means that we can tailor objects to our exact requirements without affecting the core structure of the class. It's like adding personalized stickers to a standard product, enhancing its uniqueness while keeping the original design intact.

Here is the complete code:

class Coffee:
    pass

drink = Coffee()
drink.name = "Latte"
drink.calories = 200

print(drink.name, drink.calories)
Concept of Object Oriented Programming

Working with Attributes

In this section, we'll explore how to work with attributes in Python classes. Attributes are like variables that store information about an object. We'll use the Coffee class as an example.

class Coffee:
    def showInfo1(self):
        self.name = "Latte"
        self.calories = 200
        print(self.name, self.calories)

    def showInfo2(self):
        self.name = "Americano"
        self.calories = 18
        print(self.name, self.calories)

drink1 = Coffee()
drink1.showInfo1()

drink2 = Coffee()
drink2.showInfo2()

In this code, we have a class named Coffee. Inside the class, there are two methods: showInfo1 and showInfo2. These methods set attributes (name and calories) with specific values and then print them.

When we create instances of the Coffee class, like drink1 and drink2, we can call the methods on them. This sets the attributes for each instance and prints their values.

This simple example illustrates how attributes can be used to store and manipulate data within a class, allowing us to work with different instances in a meaningful way. Here is the output of the code shown above:

OUTPUT:
Concept of Object Oriented Programming

Using Constructors to Initialize Attributes

In this section, we'll delve into the concept of using constructors to initialize attributes in Python classes. Constructors are special methods that allow us to set up initial values for object attributes. Let's explore this concept using the Coffee class.

class Coffee:
    def __init__(self, name, calories):
        self.n = name
        self.c = calories

    def showInfo(self):
        print("Name: {}\nCalories: {}".format(self.n,self.c))

drink = Coffee("Latte",200)
drink.showInfo()

In this code, the Coffee class has a constructor named __init__. The constructor takes two parameters: name and calories. Inside the constructor, the attributes n and c are assigned values based on the provided arguments.

When we create an instance of the Coffee class, like drink, we pass the values "Latte" and 200 as arguments to the constructor. The constructor initializes the attributes with these values.

The showInfo method is used to display the attributes of the instance. When we call drink.showInfo(), it prints the name and calories of the coffee.

Using constructors, we can easily set up attributes with specific values when creating instances, making our code more organized and efficient.

Concept of Object Oriented Programming