In object-oriented programming, classes and objects are the key concepts to understand how we model elements of reality and define their structure and behaviour within software. Let's look in detail at the anatomy of a class, how to create objects from it to use their properties and methods, and other key details of their relationship.
A class acts as a blueprint or mould to construct similar objects, defining their common characteristics and functionalities. It is similar to the blueprint used to construct houses in the same neighbourhood: they all share certain key attributes.
The typical components of a class are:
Attributes (properties): Variables that characterise the object. For example, for a Person
class, attributes like name
, age
, ID
, etc.
1class Person:
2 id = ""
3 name = ""
4 age = 0
Methods: Functions that define behaviours. For example, a Person
can walk()
, talk()
, eat()
, etc. They access the attributes to implement said functionality.
Constructor: Special __init__()
method that executes when instantiating the class and allows initialising attributes.
Destructor: __del__()
method that executes when deleting the instance, freeing up resources. Optional in some languages.
From the class we generate objects, which are specific instances with their own defined attributes. Let’s say the House class is the blueprint, and a specific house on a particular street is the object.
In code, we create an object by invoking the class as if it were a method:
1# Person class
2class Person:
3 def __init__(self, n, a):
4 self.name = n
5 self.age = a
6
7# Specific Person objects
8john = Person("John", 30)
9mary = Person("Mary", 35)
Each object shares the general structure and behaviour but can store different data.
We now have a Person
class and a john
object of type Person
. How do we interact with the object?
john
) and the attribute name.1john.name # "John"
2john.age # 30
1# Person class
2class Person:
3 def __init__(self, n, a):
4 self.name = n
5 self.age = a
6
7 def eat(self, food):
8 print(f"Eating {food}")
9
10# Specific Person object
11john = Person("John", 30)
12john.eat("pizza") # Prints "Eating pizza"
The john object now has its own state (properties) and behaviour (methods).
An important detail in methods is how they access the object’s attributes and other methods. Here another difference between languages comes into play:
self
. This points to the instantiated object. 1class Person:
2
3 def __init__(self, name):
4 self.name = name
5
6 def greet(self):
7 print(f"Hello! I'm {self.name}")
8
9john = Person("John")
10john.greet()
11# Prints "Hello! I'm John"
this
is used instead of self. It fulfils the same functionality of pointing to the object’s members. 1public class Person {
2
3 private String name;
4
5 public Person(String name) {
6 this.name = name;
7 }
8
9 public void greet() {
10 System.out.println("Hello! I'm " + this.name);
11 }
12}
13
14Person john = new Person("John");
15john.greet();
16# Prints "Hello! I'm John"
Classes and objects are the key concepts in OOP, allowing modelling real-world entities and generating modular, generic components of our system to construct more robust and easy to understand programmes.