Definition
A unit of encapsulated data (attributes) and behaviour (methods). An object is the ‘thing’ that exists in memory with attributes and can execute its class’s methods.
Characteristics
- Used in object-oriented programming
- Has its own values for its class’s attributes
- Can call methods defined in its class
- Is created using the constructor of its class
- Many different objects can be created from the same class
Examples
In Python, lists are objects:
names = ["Justice", "Simon", "Ash"] # a list object, an instance of the built-in list class
names.append("Shirley") # using the append method on the list object with the identifier 'names'BankAccount objects are instances of the BankAccount class:
class BankAccount:
# constructor
def __init__(self, p_account_number):
self.account_number = p_account_number
self.balance = 0
b = BankAccount(12345) # creates a BankAccount object, an instance of the BankAccount classNon-examples
- instance: ‘instance’ describes the relationship of an object to its class; every object is an instance, but ‘instance’ refers to the relationship, not the actual thing in memory
- Class: a template from which objects are created