Understanding inheritance

When a class inherits from another class, it inherits all the elements that compose the parent class, which is also known as a superclass. The class that inherits the elements is known as a subclass. For example, the Mammal subclass inherits all the properties, instance fields or instance attributes, and class fields or class attributes defined in the Animal superclass.

The Animal abstract class is the baseline for our class hierarchy. We say that it is an abstract class because we shouldn't create instances of the Animal class; instead, we must create instances of the specific subclasses of Animal. However, we must take into account that Swift doesn't allow us to declare a class as an abstract class.

We require each Animal to specify its age, so we will have to specify the age when we create any Animal, that is, any instance of any Animal subclass. The class will define an age property and display a message whenever an animal is created. The class defines three type properties that specify the number of legs, the average number of children, and the ability to fly. The first two type properties will be initialized to 0 and the last one to false. The subclasses will have to set appropriate values for these type properties. The Animal class defines the following three instance methods:

  • Print legs: This prints a representation of the specified number of legs. Guinea pigs have legs that are very different from the ones that dogs have.
  • Print children: This prints a representation of the specific average number of children.
  • Print age: This prints the animal's age.

In addition, we want to be able to compare the age of the different Animal instances using the following operators:

  • Less than (<)
  • Less than or equal to (<=)
  • Greater than (>)
  • Greater than or equal to (>=)

We have to print a message whenever we create any Animal instance. We won't create instances of the Animal class but those of its different subclasses. When we inherit from a class, we also inherit its initializer, so we can call the inherited initializer to run the initialization code for the base class. This way, it is possible to know when an instance of Animal is created, even when it is a class that we don't use to create instances. In fact, all the instances of the subclasses of Animal will be instances of Animal too.

The Mammal class inherits from Animal. We require each Mammal class to specify its age and whether it is pregnant or not when creating an instance. The class inherits the age property from the Animal superclass, so it is only necessary to add a property to specify whether it is pregnant or not. Note that we will not specify the gender at any time in order to keep things simple. If we added gender, we would need a validation to avoid a male being pregnant. Right now, our focus is on inheritance. The class displays a message whenever a mammalian animal is created, that is, whenever its initializer is executed.

Tip

Each class inherits from one class, so each new class we will define has just one superclass. In this case, we will always work with single inheritance.

The DomesticMammal class inherits from Mammal. We require each DomesticMammal class to specify its name and favorite toy. Any domestic mammal has a name and it always picks a favorite toy. Sometimes, the favorite toy is not exactly the toy we would like them to pick (our shoes, sneakers, or electronic devices), but let's keep the focus on our classes. It is necessary to add a read-only property to allow access to the name and a read/write property for the favorite toy. You never change the name of a domestic mammal, but you can force it to change its favorite toy. The class displays a message whenever a domestic mammalian animal is created.

The talk instance method will display a message indicating the domestic mammal's name concatenated with the word talk. Each subclass must make the specific domestic mammal talk in a different way. A parrot can really talk, but we will consider a dog's bark and a cat's meow as if they were talking.

The Dog class inherits from DomesticMammal and specifies 4 as the value of the number of legs. The Animal class, that is, the Mammal superclass, defines this type attribute with 0 as the value, but Dog overwrites the inherited attribute with 4. The class displays a message whenever a dog is created. The average number of children will be specified in each subclass of Dog that determines a dog breed.

We want the dogs to be able to bark, so we need a bark method. The method has to allow a dog to do the following things:

  • Bark happily just once
  • Bark happily a specific number of times
  • Bark happily at another domestic mammal with a name just once
  • Bark happily at another domestic mammal with a name a specific number of times
  • Bark angrily just once
  • Bark angrily a specific number of times
  • Bark angrily at another domestic mammal with a name just once
  • Bark angrily at another domestic mammal with a name a specific number of times

We can have just one bark method with optional arguments or many bark methods. Swift provides many mechanisms to solve the challenges of the different ways in which a dog must be able to bark.

When we call the talk method for any dog, we want it to bark happily once. We don't want to display the message defined in the talk method introduced in the DomesticMammal class. Thus, the Dog class must overwrite the inherited talk method with its own definition.

We want to know the breed and breed family to which a dog belongs. Thus, we will define both the breed and breed family type properties. Each subclass of Dog must specify the appropriate values for these type properties. In addition, two type methods will allow us to print the dog's breed and breed family.

The TerrierDog class inherits from Dog and specifies Terrier as the value for the breed family. The class displays a message whenever a TerrierDog class is created.

Finally, the SmoothFoxTerrier class inherits from TerrierDog and specifies Smooth Fox Terrier as the value for the breed. The class displays a message whenever a SmoothFoxTerrier class is created.

First, we will create a base Animal class in Swift, and then, we will use simple inheritance to create the subclasses. We will override methods and overload comparison operators to be able to compare different instances of a specific class and its subclasses. We will take advantage of polymorphism, which is a very important feature in object-oriented programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.155.216