1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

派生クラスによるさらなる上書きを禁止するsealed override

Posted at

参考

この記事は、以下の動画を参考にしています。
詳しくは、動画をご覧ください。

sealed override

あるクラスで上書きするメンバーにsealedを付けることで、そのクラスの派生クラスでさらに上書きされるのを禁止できる。

基底クラス
class BaseClass
{
    // 上書きされるメソッド
    public virtual void Method() { ... }
}
1段目の派生クラス
class FirstDerivedClass : BaseClass
{
    // 上書きするメソッドに sealed を付ける
    // このクラスの派生クラスには、上書きさせない
    public sealed override void Method() { ... }
}
2段目の派生クラス
class SecondDerivedClass : FirstDerivedClass
{
    public override void Method() { ... } // ← ビルドエラー
    // public new void Method() { ... } ← new による上書きは可能
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?