How to achieve same override experience in C# as in Java?

c#, java

Solution

No. By design. c# has `virtual` methods that you may override in subclasses. The idea is, that the possibility for override is part of the classes contract.

In the Java model, a subclass might break behavior by naming a new method the same as a base method but not providing the proper behavior.

In c# you need to be explicit about this.

Problem

Considering the following Java code: ``` public class overriding { public static void main(String[] args) { b b = new b(); a a = (a)b; a.Info(); b.Info(); } } class a { void Info() { System.out.println("I'm a"); } } class b extends a { void Info() { System.out.println("I'm b"); } } ``` And now let's try to do the same in C# ``` namespace ConsoleApplication2 { class Program { static void Main(string[] args) { b b = new b(); a a = (a)b; a.Info(); b.Info(); Console.ReadLine(); } } class a { public void Info() { Console.WriteLine("I'm a"); } } class b : a { public void Info() { Console.WriteLine("I'm b"); } } } ``` The Java example output I'm b I'm b The C# version output I'm a I'm b Is there a way to implement class b so that it prints "I'm b" twice? Please notice i'm not looking at a way to change a.

Original source

Related problems