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 of OOP
- 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. It represents a real-world entity or concept.
- Inheritance: A mechanism that allows a class to inherit properties and methods from another class.
- Polymorphism: The ability of an object to take on many forms. It allows objects of different classes to be treated as objects of a common superclass.
- Encapsulation: The concept of hiding the internal state and implementation details of an object and requiring all interaction to be performed through an object's methods.
Example
Here's a simple example of a class representing a Car
:
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 has started."
def stop_engine(self):
return f"The {self.brand} {self.model}'s engine has been turned off."
Further Reading
For more information on OOP, you can visit our Advanced OOP Tutorials.
Image
Car