Question

I am quite new to C#, so i hope if my question sounds silly please pardon my ignorance.

- I was trying out Inheritance funda with C# and find it to behave in some odd manner, so i thought to check it out with Java, and i got my expected result.

- I just want to know is there anything i am missing here.......

C# CODE :

 class Animal
 {
   public void Sound()
   {
     System.Console.WriteLine("I don't make any sound");
   }


 } 





 class Dog : Animal
 {
   public void Sound()
   {
     System.Console.WriteLine("barking");


   }  

 }


 class InheritTest
 {
   static void Main()
   {
     Animal a = new Dog();         // Implicit conversion

     Dog    d = (Dog) a;   // Explicit conversion


     a.Sound();
     d.Sound();

   }

 }

OUTPUT :

I don't make any sound
barking

JAVA CODE :

 class Animal
 {
   public void sound()
   {
     System.out.println("I don't make any sound");
   }


 } 





 class Dog extends Animal
 {
   public void sound()
   {
     System.out.println("barking");


   }  

 }


 class InheritTest
 {
   public static void main(String[] args)
   {
     Animal a = new Dog();         // Implicit conversion

     Dog    d = (Dog) a;   // Explicit conversion


     a.sound();
     d.sound();

   }

 }

OUTPUT :

barking
barking

- Now my doubt about this whole episode is.. In C# i am assigning Dog object into the Object Reference Variable a of type Animal, then when i call method Sound() on a, i should be getting the output as barking (which is the overridden method in Dog class )but instead Animal's Sound() method is called giving the output as I don't make any sound.

- But in Java things are working as expected.. Inheritance works the same way anywhere, so where did i go wrong.

I will be obliged if someone can help me out with it...... Thanks in advance.

Was it helpful?

Solution

In your first example, you're not actually overriding existing method — you're hiding it. It is a special C# mechanism, which is different from traditional method overriding. Mark the source method as virtual and override method as override for it to work as you expect. Here's a good explanation. Didn't you get a warning about that?

OTHER TIPS

Methods are virtual by default in Java (IIRC). In C#, this isn't the case.

Mark your C# methods as virtual and override them. This will produce the expected output.

Consider the code

Animal a = new Dog();
a.sound();

In order to call the sound method, the runtime needs to first resolve the actual type of object (Dog) and then call the sound method on it. If the method (sound) doesn't exist on the type (Dog), it needs to call the method on the compile-time type (Animal).

As you can see this process can take a while and can effect performance. In order to make this process more efficient C# uses something called "virtual tables" while Java uses a technique called runtime "Dynamic dispatch". What this means is that in C#, we need to mark a method as virtual to tell the compiler that it needs to perform the lookup on the underlying type. This way the program consumes a lot less memory and performs faster than if the every object had to check which actual method needs to be called. However, this also means that each method now needs to be marked as virtual to tell the compiler that we need a virtual table to lookup the method.

In summary, it is the difference in design philosophy. Java makes it easier for programmers to extend a class without worrying about which functions to mark as virtual by making all functions as virtual by default. On the other hand, C# requires you to mark function as virtual when you want the functionality to be overridden in a derived class. This also helps in eliminating unintentionally overriding functionality of base classes. (In C# methods in base class need to be marked as virtual and override in derived classes)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top