using System;
using System.Collections.Generic;
using System.Text;
public class Based
{
public string fun()
{
return " I am based";
}
}
public class Derived :Based
{
public string fun()
{
return " I am derived";
}
}
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
Based br = new Derived();;
Console.Write(br.fun());
}
}
}
Hi All , I have written small piece of code in Java and as well as in c# also.
But I am getting different outputs. Could you please explain.
In java I am getting " I am derived" , while in c# am getting " I am based" . Could you please explain to me why? and also when do we use following syntax
Baseclass obj = new Derivedclass().
In Java all methods are virtual
by default, but in C# they are not.
Therefore in C# you have to mark virtual methods manually.
Modify your code:
public class Based
{
public virtual string fun()
{
return "I am base";
}
}
public class Derived : Based
{
public override string fun()
{
return "I am derived";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With