(OOP) Object-Oriented Programming in Swift

Object-oriented programming (OOP) is a programming paradigm based on the concept of building pieces of data and their behavior into special bundles called objects that are created from blueprints named classes.

Totti, the object star.

The fundamental: objects and classes

Let’s start, do you like dogs? I love them and I will use mine for the following examples. Totti (my dog) is a German shepherd of two years, Totti is an object, an instance of the Dog class. Each dog has a series of attributes: name, sex, breed, color, etc. And also, all dogs behave similarly: they run, bark, eat, love, etc, these behaviors are represented by methods.

Then, the data stored in attributes refer to the state of the dog and the methods refer to his behavior.

It’s simple, attributes = state, methods = behavior.

Let’s represent the above in swift.

Dog class in Swift.

Note as the favoriteFood attribute was defined as optional, because not all dogs have a favorite food but some do. In addition, were created four methods that define the behavior of the any dog.

As I said before, this class is a blueprint for create any dog, basically this means that now you can create as many dogs as you want with different states (attributes value) and different behaviors (methods implementation).

Now, we can create a Totti, an instance of Dog class. But first, we should add an initializer that allows us to create dogs with different attributes:

basically, when we create a new dog we will call the init method that will change the values of the attributes to create different instances.

Now, we will create to Totti 🐶.

The object totti was created and now we can control their behavior through their methods. For example, I want Totti eat a sandwich, run to the park and then take a well-deserved one-hour nap. Simple:

Also, we can create Apolo 🐕, my friend’s dog, It has the same set of attributes as Totti, the difference is in values of these attributes: he is older, he has a different color, and is a Golden retriever.

… and so we can create dozens of dogs thanks to our blueprint Dog class.

Class Hierarchies

For now everything works fine and looks so good, but a real application contains more than just one simple class. Some of these classes might be organized into class hierarchies.

My sister has a cat, just like Totti and Apollo has a name, sex, color, age, etc, and can also run, eat, and sleep, but unlike dogs she doesn’t bark, instead she does meow.

Then we will create the Cat class to create our friend París 😺.

Cat class in Swift.

You may notice that the Cat class is exactly the same as the Dog class, just for a difference: the Cat class has a Meow method instead a Bark method in dogs.

You will think that this is very tedious, create a lot of equal code to create a class similar to another and you’re right, so I’ll show you how to use class hierarchies to avoid this.

We can define the base Animal class that should have the attributes and behaviors that are the same for both classes, this is a parent class and is called superclass. Its children: Cat and Dog classes are subclasses. Subclasses inherit state and behavior from their parent, defining only attributes or behaviors that are different, this means that the Dog subclass will have a barking method and the cat subclass the meow method.

Animal superclass in Swift.

Animal superclass was created with the common attributes that dogs and cats would have. Now, we will create the Cat and Dog subclasses.

subclass Cat in Swift.

//1: The inheritance relationship is created with the colon keyword. Cat:Animal it reads like Cat inherits from Animal .

//2: In the Cat class was created a new attribute named isCute of type Boolean, its is own of the Cat class.

//3: The initializer of the Cat class assign the value of the variable isCute and also we use this to initialize the Animal superclass within the subclass.

//4: We create the method Meow that will implement a different behavior

//5: We implement the methods of the animal superclass and we change them a little, for this, we use the keyword override .

Finally we can will create París 😻 and a generic animal.

París was created and she can meow.

An any Animal was created, its can eat but can’t meow 😢.

Thank you for reading this very basic guide, I hope to continue writing soon.