Object-oriented programming (OOP) is a programming paradigm that uses classes, attributes, and
objects to structure programs. The properties and behaviors of an object are bundled into its
attributes, which are implemented through functions. Classes can contain functions and other
classes. All of these attributes and classes can be represented by objects.
For example:
class BankAccount:
"""A class to represent a bank account."""
def __init__(self, account_number, balance):
Create or initialise objects that represents the attributes.
Info of the physical or symbolic objects
"""Initialize the bank account's account number and balance."""
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
An action or process is represented using a function.
A function contains attributes created using the constructor or
An attribute defined within the function itself.
"""Deposit money into the bank account."""
self.balance += amount
def withdraw(self, amount):
An action or process is represented using a function.
A function contains attributes created using the constructor or
An attribute defined within the function itself.
"""Withdraw money from the bank account."""
self.balance -= amount
def get_balance(self):
"""Get the current balance of the bank account."""
return self.balance
# Create a new bank account object
bank_account = BankAccount("1234567890", 1000)
# Deposit $500 into the bank account
bank_account.deposit(500)
# Withdraw $200 from the bank account
bank_account.withdraw(200)
# Get the current balance of the bank account
current_balance = bank_account.get_balance()
# Print the current balance to the console
print(f"The current balance of the bank account is {current_balance}.")
Inheritance:
Inheritance is a mechanism in object-oriented programming that allows a new class to be created
based on an existing class. The new class is called a subclass, and the existing class is called the
superclass. The subclass inherits all the attributes and methods of the superclass, and it can also add
its own attributes and methods.
A subclass with all the properties of the superclass is created with the following code:
name_of_subclass(name_of_super_class)
Inheritance is used to model real-world relationships between objects. For example, a car class can
inherit from a vehicle class. The car class will inherit all the attributes and methods of the vehicle
class, such as the make, model, year, and colour. The car class can also add its own attributes and
methods, such as the number of doors and the number of seats.
Inheritance can also be used to create hierarchies of classes. For example, a vehicle class can be
divided into subclasses such as car, truck, and motorcycle. Each subclass can inherit the attributes
and methods of the vehicle class, and it can also add its own attributes and methods.
Inheritance is a powerful tool that can help you to write more efficient, reusable, and maintainable
code.
Here is an example of inheritance in Python:
class Vehicle:
"""A class to represent a vehicle."""
def __init__(self, make, model, year, color):
"""Initialize the vehicle's make, model, year, and color."""
self.make = make
self.model = model
self.year = year
self.color = color
class Car(Vehicle):
"""A class to represent a car."""
This is a sub-class with super-class whose attributes and functions it inherits is in bracket.
def __init__(self, make, model, year, color, number_of_doors, number_of_seats):
"""Initialize the car's make, model, year, color, number of doors, and number of seats."""
super().__init__(make, model, year, color)
The super().__init__() function is used to call the constructor function of the superclass. This
ensures that the superclass's constructor function is called before the subclass's constructor
function.
self.number_of_doors = number_of_doors
self.number_of_seats = number_of_seats
# Create a new car object
car = Car("Toyota", "Camry", 2023, "red", 4, 5)
# Print the car's make, model, year, and color
print(f"The car's make is {car.make}.")
print(f"The car's model is {car.model}.")
print(f"The car's year is {car.year}.")
print(f"The car's color is {car.color}.")
Output:
The car's make is Toyota.
The car's model is Camry.
The car's year is 2023.
The car's color is red.
In this example, the Car class inherits from the Vehicle class. This means that the Car class has all
of the attributes and methods of the Vehicle class, plus its own attributes and methods.
Inheritance is a powerful tool that can help you to write more efficient, reusable, and maintainable
code. It is one of the core concepts of object-oriented programming.