c# - Make child classes unable to override method implementation -


this question has answer here:

let's have base abstract class named animal has virtual method named move.

i create child class named mammal inherits animal , defines move method.

i create child class of mammal named rabbit.

here's thing:

i not want rabbit able override implementation of move mammal has defined (child classes of mammal must not change definition of move, mammal defined).

since rabbit inherits mammal, possible "unvirtualize" move method in mammal class in order prevent inheriting classes overriding method definition in mammal?

sealed

when applied class, sealed modifier prevents other classes inheriting it. in following example, class b inherits class a, no class can inherit class b.

you can use sealed modifier on method or property overrides virtual method or property in base class. enables allow classes derive class , prevent them overriding specific virtual methods or properties.

class animal {     public virtual void move() { } } class mammal : animal {     public sealed override void move() { } } class rabbit : mammal {      public override void move() { } // error } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -