Swift - Creating Abstract Methods

Introduction

An abstract method is declared in the base class but its implementation is left to the inheriting class.

Suppose you have an area() method in the Shape class, as shown here:


class Shape {
    //stored properties
    var length:Double = 0
    var width :Double = 0

    //improvision to make the class abstract
    private init() {
        length = 0
        width = 0
    }

    func perimeter() -> Double {
        return 2 * (length + width)
    }

     //calculate the area of a shape
     func area() -> Double {}
}

The implementation of area() should be left to inheriting classes, as only specific shapes know how to calculate the area.

To improvise abstract methods, use the assert() function:


class Shape {
     //stored properties
     var length:Double = 0
     var width:Double = 0

     //improvision to make the class abstract
     private init() {
         length = 0
         width = 0
     }

     func perimeter() -> Double {
         return 2 * (length + width)
     }

     //improvision to make the method abstract
     func area() -> Double {
          assert(false, "This method must be overridden")
     }
}

The assert() function takes two arguments: a condition and a message.

When the condition evaluates to false , the program stops execution and the message is displayed.

To implement the area() method in the Rectangle class:


class Rectangle: Shape {
       //override the init() initializer
       override init() {
           super.init()
       }

       //overload the init() initializer
       init(length:Double, width:Double) {
           super.init()
           self.length = length
           self.width = width
       }

     //override the area() function
     final override func area() -> Double {
         return self.length * self.width
     }
}

area() method definition has two prefixes:

modifier Description
finalindicates that subclasses of Rectangle are not allowed to override the implementation of area()
override indicates that you are overriding the implementation of the area () method in the base class (Shape , that is)

You will now be able to use the area() method, as shown here:

Demo

class Shape {
     //stored properties
     var length:Double = 0
     var width:Double = 0

     //improvision to make the class abstract
     private init() {
         length = 0//w ww .ja  v a 2 s  .c o m
         width = 0
     }

     func perimeter() -> Double {
         return 2 * (length + width)
     }

     //improvision to make the method abstract
     func area() -> Double {
          assert(false, "This method must be overridden")
     }
}
class Rectangle: Shape {
       //override the init() initializer
       override init() {
           super.init()
       }

       //overload the init() initializer
       init(length:Double, width:Double) {
           super.init()
           self.length = length
           self.width = width
       }

     //override the area() function
     final override func area() -> Double {
         return self.length * self.width
     }
}

rectangle.length = 5
rectangle.width = 6
print(rectangle.perimeter()) //22
print(rectangle.area())      //30

Related Topic