Object-Oriented Programming (OOP) is based on the concept of modeling real-world objects and their interactions as software objects. The two fundamental building blocks of OOP are classes and objects.

A class is a blueprint or template for creating objects. It defines the attributes (also called properties or instance variables) and methods (also called behaviors or member functions) that all objects of that class will have.

For example, a class for a car might include attributes like color, model, and year, and methods like start, accelerate, and brake.

Classes are like cookie cutters, allowing you to create multiple objects that share the same structure and behavior.

An object is an instance of a class, with its own unique set of values for the attributes defined in the class. When an object is created, it is said to be instantiated from the class.

For example, if we have a Car class, we can create an object called myCar with attributes like make = “Honda”, model = “Civic”, and year = “2022”. Objects are like the cookies that are cut out using the cookie cutter - each one is unique, with its own set of characteristics, even if all cookies look the same, their are not, this is the same for objects on OOP.

In OOP, objects can interact with each other by sending messages (also called methods or member functions) to each other. For example, if we have two car objects, we can send a message to one car object to accelerate, and a message to the other car object to brake.

Overall, classes and objects are the building blocks of Object-Oriented Programming (OOP), and are essential for creating modular, reusable, and maintainable code. By defining classes that represent real-world objects and creating objects based on those classes, developers can create powerful and flexible software systems that can model complex real-world scenarios with ease.

Next we gonna understand better how to think about classes and its relation with objects.

Classes in OOP

In Object-Oriented Programming (OOP), a class is a blueprint or template for creating objects. It defines the attributes and methods that all objects of that class will have. Attributes (also called properties or instance variables) represent the data that is associated with an object, while methods (also called behaviors or member functions) represent the actions that an object can perform.

Classes provide a way to organize code into reusable and modular components. By defining a class that represents a real-world object or concept, developers can create a single code base that can be used to create multiple objects with similar properties and behaviors. This approach helps to reduce code duplication and increase maintainability.

To think and model classes in OOP, it is helpful to first identify the objects or concepts that you want to represent in your program. For example, if you are building a game, you might have classes for players, enemies, weapons, and other game objects. Once you have identified the objects, you can start to think about the attributes and methods that they will need.

Attributes

In the context of object-oriented programming (OOP), attributes are the properties or characteristics of an object. They define the data associated with an object and can be thought of as the “state” of the object at any given point in time. Attributes can be simple values, such as strings or numbers, or they can be more complex data structures, such as arrays or objects.

Attributes are defined within a class. Each object created from a class will have its own set of attribute values that reflect its current state. For example, if we have a Person class with attributes for name, age, and address, each instance of the Person class that we create will have its own values for these attributes.

We will be learning more about instance and object states in the upcoming lessons.

Attributes can be accessed and modified direct or through methods, which are functions defined within the class that allow us to manipulate the object’s data.

It’s important to note that attributes define the data associated with an object, but are not the class itself. For example, in a car class, “make”, “model”, and “year” are attributes that define the data associated with a car object, but they are not the car class itself. The class defines the structure and behavior of the object, while the attributes define the data associated with the object.

As data associated, attributes can be thought of as containers that hold data associated with an object. Each class may have none, one, or multiple attributes, which can hold either a single value or a complex value. For example, in a Car class, the make and model attributes might hold single values, like “BMW” and “X5”, while the engine attribute might hold a complex value that includes multiple properties and methods (wich means, can hold or make a reference to another object of type Engine).

Overall, attributes are a fundamental concept in OOP that allow us to represent the data and characteristics of real-world objects in our software applications.

Methods

In the context of object-oriented programming (OOP), a method is a function that is defined within a class and can be called on an object of that class. Methods allow us to define the behaviors and actions of objects, and to manipulate the data associated with those objects.

Methods are defined within a class using a specific syntax, and can take parameters just like regular functions. When a method is called on an object, it operates on the data associated with that object (properties) and can modify the object’s state. For example, a setName method defined within a Person class might take a string parameter representing a new name, and update the value of the name attribute for the object it is called on.

In OOP, all code is written in classes, but the execution is performed by objects. When we create an object from a class, we are creating an instance of that class, which has its own unique set of attribute values. We can then interact with that object by calling its methods, which perform actions based on the attribute values of the object.

In addition to modifying object state, methods can also perform other actions, such as performing calculations, interacting with external systems, read and write from files, or returning information about the object. For example, a calculateDiscount method might be defined within a Product class to calculate a discount on the price of the product, and return the discounted price as a result.

Overall, methods are a key concept in OOP that allow us to define the behaviors and actions of objects in our software applications, and to interact with the data associated with those objects in a structured and organized way.

Modeling Classes on OOP

To model classes in Object-Oriented Programming (OOP), it’s important to understand the domain of the business or problem that the software is being developed for. This involves identifying the objects, concepts, and behaviors that are relevant to the domain, and defining classes that can model those objects and behaviors in a way that is natural and intuitive to developers and end-users.

Starting an OOP Ecommerce Sample

For example, if we are developing an e-commerce website, we might identify objects like product, shopping cart, order, and customer as being relevant to the domain. We would then define classes that represent those objects, with attributes and methods that reflect their real-world characteristics and behaviors.

Let’s begin by creating a Product class, which will represent a product in our online store. Customers can use this class to search for, inspect, and add products to their shopping carts. To start with a simple example, we might define attributes for the Product class such as name, category, and price, which represent the basic information about the product.

In addition to these attributes, we can define methods for the Product class that allow us to manipulate and work with the product data. For example, we might create a setPrice method that allows us to update the price of a product, or a calculateDiscount method that applies a discount to the product price based on a given percentage or coupon code.

By creating a Product class with these attributes and methods, we can begin to model the behavior and characteristics of a real-world product in our software application.

The following image is a representation of our Product class using the [UML (Unified Modeling Language) notation. The Product class can be visually represented using the Unified Modeling Language (UML) notation, as shown in the following image:

Product class

Here’s an example of how the Product class might look in code:

class Product {
    public string $name;
    public string $category;
    public float $price;

    public function setPrice(float $newPrice): float {
        $this->price = $newPrice;
        return $newPrice;
    }

    public function calculateDiscount(float $discountPercentage): float {
        return $this->price * (1 - ($discountPercentage / 100));
    }
}
class Product {
    constructor(name, category, price) {
        this.name = name;
        this.category = category;
        this.price = price;
    }

    setPrice(newPrice) {
        this.price = newPrice;
        return newPrice;
    }

    calculateDiscount(discountPercentage) {
        return this.price * (1 - (discountPercentage / 100));
    }
}
class Product:
  def __init__(self, name, category, price):
    self.name = name
    self.category = category
    self.price = price

  def setPrice(self, newPrice):
    self.price = newPrice
    return newPrice

  def calculateDiscount(self, discountPercentage):
    return self.price * (1 - (discountPercentage / 100))
public class Product {
  public String name;
  public String category;
  public float price;

  public float setPrice(float newPrice) {
    this.price = newPrice;
    return newPrice;
  }

  public float calculateDiscount(float discountPercentage) {
    return this.price * (1 - (discountPercentage / 100));
  }
}
class Product {
  String name;
  String category;
  double price;

  double setPrice(double newPrice) {
    this.price = newPrice;
    return newPrice;
  }

  double calculateDiscount(double discountPercentage) {
    return this.price * (1 - (discountPercentage / 100));
  }
}

In the next lesson you will learn how to create objects based on our Product class and start leveraging its functionality.