Classes and objects

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.

Anatomy of a class

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.


Creating objects

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.

Using properties and methods

We now have a Person class and a john object of type Person. How do we interact with the object?

  • Properties: It is possible to access the value of an object attribute using the object reference (john) and the attribute name.
1john.name  # "John"
2john.age   # 30
  • Methods: In the same way as accessing attributes but adding parentheses inside which arguments are passed if it takes any.
 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).


Self vs This

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: In Python, attributes and methods are accessed within the class by prepending 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: In Java or C#, 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"

Conclusion

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.