Definition
A variable associated with either a class or an object created from a class.
Characteristics
- Used in object-oriented programming
- Can be either public or private, controlling access from outside the class
- Class-level attributes are shared across all instances
- Instance-level attributes are unique to each object
- Could be initialised with arguments passed to the constructor or with default values
- Could be accessed indirectly using a get method
- Could be changed using a set method
Examples
Instance-level attribute
In Python, attributes are declared and initialised in the constructor.
class BankAccount:
def __init__(self, p_owner, p_initial_balance):
self.owner = p_owner
self.__balance = p_initial_balance
self.account_type = "Savings"Class-level attribute
Class-level attributes are defined on the class itself rather than on individual objects. They are shared across all instances of a class.
In this BankAccount class, every instance can access interest_rate.
class BankAccount:
interest_rate = 0.03
def __init__(self, owner):
self.owner = ownerModify a public attribute
The values of public attributes can be changed by accessing them directly.
class BankAccount:
...
b = BankAccount("Timmy Banks", 500)
b.owner = "Timmy Gold"Modify an attribute with a method
The values of attributes can be changed using methods, including setters (when they have been implemented).
class BankAccount:
...
def deposit(self, p_amount):
if p_amount > 0:
self.__balance = self.__balance + p_amount
b = BankAccount("Timmy Banks", 500)
b.deposit(50)Declare an attribute (Java)
Most programming languages, including Java, declare attributes at the start of the class definition rather than in the constructor.
public class BankAccount {
// attributes
public String name;
private double balance;
public String accountType = "Savings";
// constructor
public BankAccount(String pOwner, double pInitialBalance) {
this.owner = pOwner;
this.balance = pInitialBalance;
}
}Non-examples
Parameter
Parameters are not attributes.
class BankAccount():
def __init__(self, p_owner, p_initial_balance):
self.owner = p_owner
self.__balance = p_initial_balance
self.account_type = "Savings"Variable
Variables are not attributes; e.g., heal_amount is a local variable and not an attribute.
class Enemy:
...
def heal(self):
heal_amount = 10
self.health = self.health + heal_amountClass
A class defines the template used to create an entire object.
Method
Methods define the behaviour of a class.