Characteristics of object-oriented programming concepts

Object-oriented programming (OOP) is a way of writing computer programs that focuses on organising code into reusable, modular components called objects. Objects are like real-world things that have characteristics (called attirbutes) and can do things (called methods). With OOP, we create classes to define the objects, and these classes serve as blueprints for creating multiple objects with similar characteristics.

Class: A class is a blueprint or template for creating objects in object-oriented programming. It defines a set of attributes (variables/data) and methods (behaviours) that objects of that class will have.

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

    def start_engine(self):
        print("Engine started!")

Creating objects (instances) of the Car class

car1 = Car("Tesla", "Red")
car2 = Car("BMW", "Blue")

print(car1.brand) # Output: "Tesla"
car2.start_engine() # Output: "Engine started!"

Object: An object is an instance of a class. It represents a specific entity with its own unique data and behavior based on the class definition. In the example above car1 and car2 are objects.

NOTE: The purpose of the `__init__` method is to initialize the attributes of the object. The `self` parameter allows access to those attributes within the class methods.

Attribute: An attribute is a variable associated with an object. It holds data specific to that object.

Example:

# Car class with attributes
class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

# Creating an instance of the Car class with attributes
car1 = Car("Tesla", "Red")
print(car1.brand) # Output: "Tesla"
print(car1.color) # Output: "Red"

Method: A method is a function defined within a class and is responsible for performing actions or providing behavior specific to objects of that class.

Example:

# Car class with a method
class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

    def start_engine(self):
        print("Engine started!")

# Creating an instance of the Car class and calling the method
car1 = Car("Tesla", "Red")
car1.start_engine() # Output: "Engine started!"

Inheritance: Inheritance is a mechanism in object-oriented programming where one class (called the child class) inherits the attributes and behaviors of another class (called the parent class). It allows for code reuse and the creation of a hierarchy of classes.

Example:

# Parent class
class Vehicle:
    def __init__(self, brand):
        self.brand = brand

    def start(self):
        print("Vehicle started!")

# Child class inheriting from the parent class
class Car(Vehicle):
    def __init__(self, brand, color):
        super().__init__(brand)
        self.color = color

    def start_engine(self):
        print(f"{self.brand} engine started!")


# Creating an instance of the Car class and accessing inherited attributes and methods
car1 = Car("Tesla", "Red")
print(car1.brand) # Output: "Tesla"
car1.start() # Output: "Vehicle started!"
car1.start_engine() # Output: "Tesla engine started!"

Abstraction: Abstraction is the concept of hiding the complex reality while exposing only the necessary parts. It means representing essential features without including the background details.

Polymorphism: Polymorphism is the ability of objects of different classes to respond to the same method or attribute differently based on their own class’s implementation. It allows methods to be defined in multiple classes with the same name but with different behaviors.

Example:

# Parent class
class Animal:
    def sound(self):
        pass

# Child classes with their own implementation of the sound method
class Dog(Animal):
    def sound(self):
        print("Woof!")

class Cat(Animal):
    def sound(self):
        print("Meow!")

# Creating instances of different classes and calling the same method
doggo = Dog()
catto = Cat()

doggo.sound() # Output: "Woof!"
catto.sound() # Output: "Meow!"

Instantiation: Instantiation is the process of creating an object (an instance) of a class. When you instantiate a class, you are essentially creating an actual object in memory based on that blueprint.

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

# This line is where instantiation is taking place – car1 is being instiated.
car1 = Car(“Tesla”, “Red”)

Encapsulation: Encapsulation is the practice of making attributes or methods private within a class to hide and protect them from external access or modification, ensuring an object’s internal state is safely contained.

In a `BankAccount` class, you might have a private attribute `balance` and a public method `deposit(amount)` to add money. Direct access to `balance` is restricted, helping to keep it safe from unauthorized changes.

To understand how this works in Python watch this video: