Inheritance

The concept of inheritance is a basic object-oriented development concept. Inheritance allows a class to be defined as having a certain set of characteristics and then other classes can be derived from that class. The derived class inherits all of the features of the class it is inheriting from (unless the derived class overrides those characteristics) and then usually adds additional characteristics of its own.

With inheritance, we can create what is known as a class hierarchy. In a class hierarchy, the class at the top of the hierarchy is known as the base class and the derived classes are known as subclasses. We are not limited to only creating subclasses from a base class, we can also create subclasses from other subclasses. The class that a subclass is derived from is known as the parent or superclass. In Swift, a class can have only one parent class. This is known as single inheritance.

Inheritance is one of the fundamental differences that separate classes from structures. Classes can be derived from a parent or superclass, but a structure cannot.

Subclasses can call and access the properties, methods, and subscripts of their superclass. They can also override the properties, methods, and subscripts of their superclass. Subclasses can add property observers to properties that they inherit from a superclass, so they can be notified when the values of the properties change. Let's look at an example that illustrates how inheritance works in Swift.

We will start off by defining a base class named Plant. The Plant class will have two properties, height and age. It will also have one method, growHeight(). The height property will represent the height of the plant, the age property will represent the age of the plant, and the growHeight() method will be used to increase the height of the plant. Here is how we would define the Plant class:

class Plant { 
  var height = 0.0  
  var age = 0 
  func growHeight(inches: Double) {  
    height += inches; 
  } 
} 

Now that we have our Plant base class, let's see how we would define a subclass of it. We will name this subclass Tree. The Tree class will inherit the age and height properties of the Plant class and add one more property named limbs. It will also inherit the growHeight() method of the Plant class and add two more methods: limbGrow(), where new limbs are grown, and limbFall(), where limbs fall off the tree. Let's have a look at the following code:

class Tree: Plant {  
  var limbs = 0  
  func limbGrow() { 
    self.limbs += 1 
  } 
  func limbFall() {  
    self.limbs -= 1 
  } 
} 

We indicate that a class has a superclass by adding a colon and the name of the superclass to the end of the class definition. In this example, we indicated that the Tree class has a superclass named Plant.

Now, let's look at how we could use the Tree class that inherited the age and height properties from the Plant class:

var tree = Tree()  
tree.age = 5 
tree.height = 4  
tree.limbGrow() 
tree.limbGrow() 

The preceding example begins by creating an instance of the Tree class. We then set the age and height properties to 5 and 4, respectively, and add two limbs to the tree by calling the limbGrow() method twice.

We now have a base class named Plant that has a subclass named Tree. This means that the super (or parent) class of Tree is the Plant class. This also means that one of the subclasses (or child classes) of Plant is named Tree. There are, however, lots of different kinds of trees in the world. Let's create two subclasses from the Tree class. These subclasses will be the PineTree class and the OakTree class:

 

class PineTree: Tree {  
  var needles = 0 
} 
 
class OakTree: Tree {  
  var leaves = 0 
} 

The class hierarchy now looks like this:

It is important to keep in mind that in Swift, a class can have multiple subclasses; however, a class can have only one superclass. There are times when a subclass needs to provide its own implementation of a method or property that it inherited from its superclass. This is known as overriding.

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

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