Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (often known as methods).

Key Concepts

  • Class: A blueprint for creating objects. It defines the properties and behaviors that the objects of the class will have.
  • Object: An instance of a class. An object has a state and behavior.
  • Inheritance: A way to create a new class from an existing class. The new class inherits the properties and methods of the parent class.
  • Polymorphism: The ability of an object to take on many forms. It is usually used to handle different behaviors for different data types using a unified interface.
  • Encapsulation: The concept of bundling the data (variables) and the methods that operate on the data into a single unit called a class.

Example

Let's take a simple example of a Car class.

class Car:
    def __init__(self, brand, model, year):
        self.brand = brand
        self.model = model
        self.year = year

    def start_engine(self):
        return f"The {self.brand} {self.model}'s engine is now running."

    def stop_engine(self):
        return f"The {self.brand} {self.model}'s engine is now turned off."

Usage

You can create an object of the Car class and use its methods like this:

my_car = Car("Toyota", "Corolla", 2020)
print(my_car.start_engine())
print(my_car.stop_engine())

Further Reading

For more information on OOP, you can visit our OOP tutorials page.

Images

Toyota Corolla