Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# vs Java - why virtual keyword is necessary?

I've started to learn some C# and I came accross a troubling matter: the virtual methods. Is there any motivation for such keyword to be necessary?

A simple polymorphism in Java does not requre virtual keyword to work, even the Override adnotations are optional:

package figures;

public class Figures {
    public static void main(String[] args) {
        Figure figure = new Figure();
        Circle circle = new Circle();
        Triangle triangle = new Triangle();
        Figure []arrayOfFigures = {figure, circle, triangle};
        for (int i = 0; i < 3; i++){
            arrayOfFigures[i].introduceYourself();
        }
    }
}

class Figure {
    public void introduceYourself(){
        System.out.println("I am just a figure.");
    }
}

class Circle extends Figure {
    @Override
    public void introduceYourself() {
        System.out.println("I am a circle.");
    }
}

class Triangle extends Figure {
    @Override
    public void introduceYourself() {
        System.out.println("I am a triangle.");
    }
}

While in C# the same example requires both virtual and override keywords to work:

namespace Figures
{
    class Figures
    {
        static void Main(string[] args)
        {
            Figure figure = new Figure();
            Circle circle = new Circle();
            Triangle triangle = new Triangle();
            Figure[] arrayOfFigures = { figure, circle, triangle };
            for (int i = 0; i < 3; i++)
            {
                arrayOfFigures[i].IntroduceYourself();
            }
        }
    }
}

    class Figure
    {
        public virtual void IntroduceYourself()
        {
            System.Console.WriteLine("I am just a simple figure.");
        }
    }

    class Circle : Figure
    {
        public override void IntroduceYourself()
        {
            System.Console.WriteLine("I am a circle.");
        }
    }

    class Triangle : Figure
    {
        public override void IntroduceYourself()
        {
            System.Console.WriteLine("I am a triangle.");
        }
    }

Usually there is a motivation to introduce some keywords to languages. As C# was created after Java and many other object oriented languages, I Wonder if there was a reason to introduce obligatory (for polymorphism to work) virtual and override keywords?

like image 708
3yakuya Avatar asked Feb 14 '14 16:02

3yakuya


1 Answers

In Java, methods are virtual by default. In C#, they are not, and must be marked as virtual in order for polymorphism to work.

It's a difference in philosophy. Java's philosophy is that a virtual-by-default approach makes it easy for you to extend classes at-will. C#, on the other hand, figures that you should only have virtual functions when you explicitly say you need them (so a function can be overriden only if explicitly allowed to.) There's a slight performance consideration, since a virtual function requires an additional level of indirection.

like image 68
Matt Kline Avatar answered Oct 04 '22 06:10

Matt Kline