How to achieve same override experience in C# as in Java? -
considering following java code:
public class overriding { public static void main(string[] args) { b b = new b(); a = (a)b; a.info(); b.info(); } } class { void info() { system.out.println("i'm a"); } } class b extends { void info() { system.out.println("i'm b"); } } and let's try same in c#
namespace consoleapplication2 { class program { static void main(string[] args) { b b = new b(); a = (a)b; a.info(); b.info(); console.readline(); } } class { public void info() { console.writeline("i'm a"); } } class b : { 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 way implement class b prints "i'm b" twice? please notice i'm not looking @ way change a.
no. design. c# has virtual methods may override in subclasses. idea is, possibility override part of classes contract.
in java model, subclass might break behavior naming new method same base method not providing proper behavior.
in c# need explicit this.
Comments
Post a Comment