O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required