LoginSignup
0
1

More than 3 years have passed since last update.

finalキーワード

Posted at

オーバーライド可能な要素の前にfinalキーワードを記述することで、その要素がサブクラスでオーバーライドされることを禁止できます。

class SuperClass {
    func overridableMethod() {}

    final func finalMethod() {}
}

class SubClass: SuperClass {
    override func overridableMethod() {}

    // オーバーライド不可能なためエラー
    override func finalMethod() {}
}

クラス自体にfinalキーワードを付与することで、そのクラスを継承したクラスを定義することを禁止することができます。

class Class1 {}

class class1: Class1 {}

final class FinalClass {}

// 継承不可能なためコンパイルエラー
class class2: FinalClass {}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1