Question

I think child class can override parent method which is not virtual

class Parent {
    public  void hello() {
        Console.WriteLine("Hello Parent");
    }
}
class Child:Parent{
    public void hello() {
        Console.WriteLine("Hello Child");
    }
    static void Main() {
        Parent p = new Child();
        Child c = new Child();
        p.hello();  // Hello Parent
        c.hello();  // Hello Child
    }
}

So what different between virtual and not virtual in a parent method ???

Was it helpful?

Solution 2

There is a big difference. I suggest you read this and carefully go through the examples.

To summarize, what you're dealing with is the difference between method overriding vs method hiding.

Method overriding allows a base type to access the behavior of the derived type's overridden functionality. This is what you're doing when you use the override keyword on a derived type's method where the base type's method is marked with the virtual keyword. On the other hand, using the same method signature on a more derived where the base type's method is not marked as virtual is what's known as method hiding. You can actually do this without using the new keyword, but you will get a compiler warning.

Method hiding loses the polymorphic benefits that method overriding provides, in that the methods of the derived class will use the "new" behavior of the hidden method, but base class will continue to use the base class's version of the method. The results of method hiding can seem pretty unintuitive and typically the effects are undesirable in OOP, but it does have its place. For instance, one of my favorite uses of method hiding is to allow for easier unit testing of protected methods.

The following code example is taken directly from the first link and succinctly illustrates one of the ways which method overriding differs from method hiding:

class Program
{
    static void Main(string[] args)
    {
        BaseClass bc = new BaseClass();
        DerivedClass dc = new DerivedClass();
        BaseClass bcdc = new DerivedClass();

        // The following two calls do what you would expect. They call
        // the methods that are defined in BaseClass.
        bc.Method1();
        bc.Method2();
        // Output:
        // Base - Method1
        // Base - Method2


        // The following two calls do what you would expect. They call
        // the methods that are defined in DerivedClass.
        dc.Method1();
        dc.Method2();
        // Output:
        // Derived - Method1
        // Derived - Method2


        // The following two calls produce different results, depending 
        // on whether override (Method1) or new (Method2) is used.
        bcdc.Method1();
        bcdc.Method2();
        // Output:
        // Derived - Method1
        // Base - Method2
    }
}

class BaseClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Base - Method1");
    }

    public virtual void Method2()
    {
        Console.WriteLine("Base - Method2");
    }
}

class DerivedClass : BaseClass
{
    public override void Method1()
    {
        Console.WriteLine("Derived - Method1");
    }

    public new void Method2()
    {
        Console.WriteLine("Derived - Method2");
    }
}

OTHER TIPS

In C#, virtual methods support polymorphism, by using a combination of the virtual and override keywords. With the virtual keyword on the base class method and the override keyword on the method in the derived class, both methods are said to be virtual.

Methods that don’t have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual.

When a virtual method is invoked on an object, the run-time type of the object is used to determine which implementation of the method to use.

When a non-virtual method is invoked on an object, the compile-time type of the object is used to determine which implementation of the method to use.

In this case, you can use the new keyword

public new void hello()
{
    Console.WriteLine("Hello Child");
}

Text taken from here

Read more about when to use override and new keyword

The method hello() in the child class not override the hello method in the parent class. Child method just hide the implementation in parent class

Only methods marked as virtual can be overridden. Others can be hidden (using the new keyword), which isn't the same thing at all.

class Parent
{
    public virtual int MyMethod()
    {
        return 1;
    }
}

class Child : Parent
{
    public override int MyMethod()
    {
        return 2;
    }
}

class OtherChild : Parent
{
    public new int MyMethod()
    {
        return 3;
    }
}
// ...

Parent p = new Parent();
Parent c = new Child();
Parent oc = new OtherChild();

int result;

result = p.MyMethod(); // will return 1
result = c.MyMethod(); // will return 2
result = oc.MyMethod(); // will return 1

In the example above notice that every variables are declared as Parent. Calling the MyMethod on the first will simply return the basic implementation. Calling it on the second one calls the override found in Child class. The third one, however, being declared Parent, doesn't know about the implementation of MyMethod within OtherChild class and calls the one it knows: in Parent.

If you want to learn more on polymorphism, I suggest you have a look at this article on Wikipedia:

Polymorphism (computer science)

problem is with your testing code, power of virtual comes this way (if you set your method to virtual):

Parent p = new Parent();
Parent c = new Child();

Console.WriteLine(p.hello()); //Prints Hello Parent
Console.WriteLine(c.hello()); //Prints Hello Child

if you keep it unvirtual, both lines would print hello parent

kinda real world example is this:

Vehicle p = new Vehicle();
Vehicle c = new Car();

Console.WriteLine(p.Drive()); //Prints "Default Vehicle"
Console.WriteLine(c.Drive()); //Prints "Car"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top