5

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.

Community
  • 1
  • 1
Dorus
  • 7,276
  • 1
  • 30
  • 36
  • 12
    This isn't overloading, this is overriding, and you need the keyword override in C# to do it. In Java methods with the same signature override always. – Samuel Jul 18 '13 at 11:26
  • overloading is when two methods share the same name, but different parameters; overriding is when they share the same method signature. To override in java, you need the `@Override` annotation, I believe C# has an equivallent of that. – Shark Jul 18 '13 at 11:27
  • 1
    @Shark You don't need the `@Override` annotation. All that does is throw a compiler error if the method isn't overriding a superclass method. For example, if I had @Override on a method and then changed the signature of the overridden method, @Override would throw a compiler error. – Samuel Jul 18 '13 at 11:29
  • 1
    @Samuel: I guess i learned something today :) – Shark Jul 18 '13 at 11:30
  • @Samuel: Correct, I've modified the question. – Dorus Jul 18 '13 at 11:33
  • [this so post](http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c) also discusses the topic. – collapsar Jul 18 '13 at 11:33

5 Answers5

9

In Java, methods are virtual by default. In C# they are not, and you need to use the keywords "virtual" and "override" for the method declarations in classes a and b, respectively.

Eyvind
  • 5,221
  • 5
  • 40
  • 59
  • Does this mean it's not possible without changing a? – Dorus Jul 18 '13 at 11:31
  • 1
    @Dorus If you don't have permission to make changes to the base class, then no, you can't make a method virtual and, therefore, override its behavior. This is because the vendor of a base class needs to have control over what can and can't be overridden, in order to design the class appropriately. – Theodoros Chatzigiannakis Jul 18 '13 at 11:37
  • You can also use the "new" keyword, but this has limitations. See my answer above – Dave B 84 Jul 18 '13 at 11:41
5

In C# version, you need to use override keyword in the class b method, and also you need to make the method in class a virtual explicitly. In Java, methods are virtual by default. That's not the case in C#. You need to tell that explicitly:

class a
{
    public virtual void Info()
    {
        Console.WriteLine("I'm a");
    }
}

class b : a
{
    public override void Info()
    {
        Console.WriteLine("I'm b");
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

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.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
1

One major difference between Java and C# is that in Java, methods are virtual by default. To do the same in C#, you need to mark the method a.Info as virtual and then use the override keyword in b:

class a
{
    public virtual void Info()
    {
        Console.WriteLine("I'm a");
    }
}

class b : a
{
    public override void Info()
    {
        Console.WriteLine("I'm b");
    }
}

UPDATE

You can also use the new keyword on class b without needing the virtual keyword on a:

class a
{
    public void Info()
    {
        Console.WriteLine("I'm a");
    }
}

class b : a
{
    public new void Info()
    {
        Console.WriteLine("I'm b");
    }
}

However, this will only produce "I'm b" when the object is of type b, but will say "I'm a" when it is cast back to b:

b myB= new b();
myB.Info();  // This will say "I'm b"

a myA = (a)myB;
myA.Info();  // This will say "I'm a" even though it's really a b object.

See MSDN more info

Dave B 84
  • 568
  • 1
  • 5
  • 14
  • Adding 'new' removes the compile warning about hiding a inherited method, but doesn't change any behavior. – Dorus Jul 18 '13 at 11:54
0

In Java , we are not using any keywords for method overloading and method overriding. While in C#, you have to use override for method overriding explicitly.

 public override void Info()
    {
        Console.WriteLine("I'm b");
    }
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17