What is the difference between final Class and Class?

前端 未结 7 2161
名媛妹妹
名媛妹妹 2020-12-25 09:40

What is the difference between final Class and Class?

final class A {

}

class B {    

}
相关标签:
7条回答
  • 2020-12-25 10:09

    Other answers are already given enough understanding about final keyword. I want to explain by some example.

    Consider below example without final Keyword.

    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 above code it allow to overwrite the varible of it's super class. While Class with final keyword don't allow it. Refer below 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)
        }
    }
    

    Above code will give error inheritance from a final class A class B

    0 讨论(0)
  • 2020-12-25 10:11

    Besides final declaration does not need to be overridden also has better performance than no use final, because final disable dynamic dispatch at runtime which save runtime overhead.

    0 讨论(0)
  • 2020-12-25 10:16

    Classes marked with final can not be overridden.


    Why should one care at all whether class can or can't be overridden?

    There are two things to consider:

    1. As an API designer/developer, you might have a class in your Framework that can be abused or misused when subclassed. final prevents class to be subclassed—mission accomplished. You can also use final to mark methods, properties, and even subscripts of non-final classes. This will have same effect, but for particular part of the class.
    2. Marking class as final also tells Swift compiler that method should be called directly (static dispatch) rather than looking up a function from a method table (dynamic dispatch). This reduces function call overhead and gives you extra performance. You can read more on this on Swift Developer Blog.
    0 讨论(0)
  • 2020-12-25 10:25

    Use final when you know that a declaration does not need to be overridden. The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.

    Read more:

    https://developer.apple.com/swift/blog/?id=27

    0 讨论(0)
  • 2020-12-25 10:26

    Final means that no one can inherit from this class.

    0 讨论(0)
  • 2020-12-25 10:29

    Hoping I can boil down the other answers into this SSCCE that helped me:

              open class Open {}     // Anyone can see, anything can subclass
    public         class Normal {}   // Anyone can see, internal can subclass
    internal       class Internal {} // Internal can see, internal can subclass
    public   final class Final {}    // Anyone can see, nothing can subclass
    

    In your project/module:

    class SubOpen:     Open {}     // OK
    class SubNormal:   Normal {}   // OK
    class SubInternal: Internal {} // OK
    class SubFinal:    Final {}    // Error: Can't subclass
    

    In some other project/module:

    class SubOpen:     Open {}     // OK
    class SubNormal:   Normal {}   // Error: Can't subclass
    class SubInternal: Internal {} // Error: `Internal` type not found
    class SubFinal:    Final {}    // Error: Can't subclass
    
    0 讨论(0)
提交回复
热议问题