Swift - Custom Type Overloading Methods

Introduction

Besides overloading initializers, you can overload methods.

The following code creates another subclass called Circle that inherits from Shape:


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")
     }
}
class  Circle :  Shape  {

    //initializer
    init(radius:Double) {
        super.init()
        self.width = radius * 2
        self.length = self.width
    }

    //override the perimeter() function
    override func perimeter() -> Double {
        return 2 * M_PI * (self.width/2)
    }

    //overload the perimeter() function
    func perimeter(#radius:Double) -> Double {

        //adjust the length and width accordingly
        self.length = radius * 2
        self.width = self.length

        return 2 * M_PI * radius
        }

        //override the area() function
        override func area() -> Double {
            return M_PI * pow(self.length / 2, 2)
        }
   }
}

You can now use the Circle class as follows:

var circle = Circle(radius: 6.8)
print(circle.perimeter())          
print(circle.area())                

//need to specify the radius label
print(circle.perimeter(radius:7.8)) 

//call the perimter() method above
// changes the radius
print(circle.area())                

Because the perimeter() method is overloaded, you can call it either with no argument or with one argument.

Demo

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

     //improvision to make the class abstract
     private init() {
         length = 0//from w ww . ja  v  a  2  s . com
         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  Circle :  Shape  {

    //initializer
    init(radius:Double) {
        super.init()
        self.width = radius * 2
        self.length = self.width
    }

    //override the perimeter() function
    override func perimeter() -> Double {
        return 2 * M_PI * (self.width/2)
    }

    //overload the perimeter() function
    func perimeter(#radius:Double) -> Double {

        //adjust the length and width accordingly
        self.length = radius * 2
        self.width = self.length

        return 2 * M_PI * radius
        }

        //override the area() function
        override func area() -> Double {
            return M_PI * pow(self.length / 2, 2)
        }
   }
}
var circle = Circle(radius: 6.8)
print(circle.perimeter())           
print(circle.area())               

//need to specify the radius label
print(circle.perimeter(radius:7.8)) 

//call the perimter() method above
// changes the radius
print(circle.area())