7.13. Sealed methods (Java final methods)

It will be wise to make sure you understand section 7.10 before reading this section.

C# sealed methods are simply Java final methods – methods which cannot be overridden in subclasses. Here is an example of how an attempt to override a sealed method will result in a compilation error:

 1: using System;
 2:
 3: class GrandChild:Child{
 4:   public static void Main(){
 5:   }
 6:
 7:   // this will cause a compilation error.
 8:   public override void DoSomething(){
 9:     Console.WriteLine("running version 3");
10:   }
11: }
12:
13: class Child:Parent{
14:   public override sealed void DoSomething(){ 15: Console.WriteLine("running version 2"); 16: } 17: } 18: 19: class Parent{ 20: public virtual void DoSomething(){ 21: Console.WriteLine("running ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.