What is the difference between the final class and the class?

What is the difference between the final class and the class?

final class A {

}

class B {    

}
+16
source share
6 answers

Final is a class modifier that prevents its inheritance or overriding. From Apple documentation

You can prevent overriding a method, property, or index by marking it final . Do this by writing the final modifier before the keywords of the method, property, or index introker like final var, final func, final class func, and final index).

, . , , , .

final, ( ). .

+32

final .


, ?

:

  • / API, Framework, . final -. final , . , .
  • final Swift, ( ), ( ). . Swift Developer.
+23
+18

Final , .

+5

, final , , final, final , .

+5

Other answers already provide enough understanding about the final keyword. I want to explain with some example.

Let's look at the example below without a finalkeyword.

class A {
   public var name: String
   var breed: String

    init(name: String, breed: String) {
        self.name = name
        self.breed = breed
    }
}
class B:A{
    override init(name: String, breed: String) {
        super.init(name: name, breed: breed)
    }
}

In the above code, this allows you to overwrite the variable of this superclass. A class with the last keyword does not allow. See below for an example.

final class A {
   public var name: String
   var breed: String

    init(name: String, breed: String) {
        self.name = name
        self.breed = breed
    }
}

class B:A{
    **//ERROR:inheritance from a final class 'A' class B**
    override init(name: String, breed: String) {
        super.init(name: name, breed: breed)
    }
}

The code above will inherit errors from the final class A of class B

0
source

Source: https://habr.com/ru/post/1685068/


All Articles