Class Vs Final Vs Static

Last updated on September 14th, 2020

Class

  • Can be used against
    1. Computed properties in Classes
    2. Methods in Classes
  • Using this causes
    1. The Property/Method to become part of the Type instead of Instance.
  • Example
class Apple {
    class var price : Double { return 1.5 }
    class func weight() -> Int { return 1 }
}

Final

  • Can be used against
    1. Stored/Computed Properties in Classes
    2. Methods in Classes
    3. Classes
  • Using this causes
    1. Prevention of further overriding of the Property/Method in a Subclass
    2. Prevention of Sub classing in the case of Classes
  •  Examples
class Apple {
    final var price : Double = 1.5    
    final func weight() -> Int { return 1 }
}

final class Orange {
    var price : Double = 2.5
    func weight() -> Int { return 2 }
}

Static

  • Can be used against
    1. Stored/Computed Properties in Classes, Structures, Enumerations
    2. Methods in Classes, Structures, Enumerations
  • Using this causes
    1. The Property/Method to become part of the Type instead of Instance (similar to Class keyword)
    2. Prevention of further overriding of the Property/Method (similar to Final keyword)
  • Example
struct Mango {
    static var price: Double = 3.5
    static func weight() -> Int { return 3 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *