Can you allow class to be inherited, but prevent the method from being over-ridden?

Yes, just leave the class public and make the method sealed.

Showing Answers 1 - 3 of 3 Answers

samiksc

  • Jul 10th, 2007
 

Use 'private' access specified to prevent a method from being overridden in the derived class.
The 'sealed' keyword can be applied to methods along with the 'override' keyword. That is, the keyword prevents further overridings of the method - class base declares a method M to be virtual. class d1 (which is derived from base) overrides the method along with 'sealed override' keywords. class d2 (which is derived from d1) cannot override M in this case.
However if you apply the 'sealed' keyword without override keyword you get a com-piler error

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions