Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve same override experience in C# as in Java?

Tags:

java

c#

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.

like image 259
Dorus Avatar asked Jul 18 '13 11:07

Dorus


People also ask

How is function overriding implemented in C?

How to access Overridden Functions in C++ You must use the scope resolution operator, “::” to access the overridden function. Another way to access the overridden function is by using the pointer of the base class to point to an object of the derived class and calling the function through the pointer.

Is overriding possible in C?

Master C and Embedded C Programming- Learn as you goThe function overriding is the most common feature of C++. Basically function overriding means redefine a function which is present in the base class, also be defined in the derived class. So the function signatures are the same but the behavior will be different.

Can we override already overridden method?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

Can you have two functions with the same name in C?

Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures. This feature is present in most of the Object Oriented Languages such as C++ and Java.


2 Answers

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.

like image 139
Eyvind Avatar answered Sep 27 '22 23:09

Eyvind


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");
    }
}
like image 27
Rohit Jain Avatar answered Sep 27 '22 22:09

Rohit Jain