Pregunta

I am new to C# (coming from C++)

  1. Can I declare a method to be virtual and override ?(In C++ the method can be virtual in base and derived class as well)

  2. The explanation about new method keyword in my textbook says:

Sometimes you may want to create your own implementation of a method that exists in a base class. The Line class does this by declaring its own print() method. The Line print() method hides the DrawingObject print() method. The effect is the Parent print() method will not be called, unless we do something special to make sure it is called. Notice the new modifier on the Line class print() method. This enables this method to hide the DrawingObject class print() method and explicitly states your intention that you don't want polymorphism to occur. Without the new modifier, the compiler will produce a warning to draw your attention to this.

What is the influence of it if?

  1. The base class method is virtual and the derived new?
  2. The base class method is not virtual and the derived is new?
  3. What happens if I don`t use new keyword for the method in derived class
  4. How does polymorphism happen if the method is not virtual in the base class?
  5. Why "Without the new modifier, the compiler will produce a warning"?

The questions refer the following code.

using System;

public class DrawingObject
{
    public virtual void Draw()
    {
        Console.WriteLine("I'm just a generic drawing object.");
    }
    public void print()
    {
        Console.WriteLine("I'm a Parent Class.");
    }
}
public class Line : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Line.");
    }
    public new void print()
    {
        base.print();
        Console.WriteLine("I'm a Child Class.");
    }
}

public class Circle : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Circle.");
    }
}

public class Square : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Square.");
    }
}
¿Fue útil?

Solución

  1. If instance member declared as virtual in base class, then it will continue to remain virtual in derived classes until it will not be marked as sealed.
  2. Avoid to using new keyword because it breaks polymorphism (see your example with Draw method - it should be marked as virtual).

Example of broken polymorphism:

public class Base
{
    public void Method()
    {
        Console.WriteLine("Base.Method()");
    }
}

public class Derived : Base
{
    public new void Method()
    {
        Console.WriteLine("Derived.Method()");
    }
}

Derived obj = new Derived();
obj.Method(); // Will output "Derived.Method()"
((Base)obj).Method() // Will output "Base.Method()"

It is not you usually expect when you works with class hierarchies. It should not make a difference whether you call Method() from base class or from derived one.

Otros consejos

virtual (C# Reference)

You cannot use the virtual modifier with the static, abstract, private, or override modifiers.

Most of your other questions can be answered from Knowing When to Use Override and New Keywords (C# Programming Guide)

Yes, you can mark a method with virtual keyword and then using override override it in derived class.

  1. If you have variable of derived type - new method will be called. If you have variable of base type (but with derived type really in it) virtual method from base class will be called.
  2. The same.
  3. If you mark it with override you will get classical polymorphism.
  4. Method should be virtual it base class to use polymorphism.
  5. It is a warning for you for the case if you occasianlly unintentionally create a new method which hides base class version. You should use new to make your intention explicit and be sure that it's not a mistake.

The new keyword in the derived class is called "shadowing" the base class' method, in essence it hides the base class' implementation of the method. The warning message you are seeing if you omit the new keyword is the compiler telling you that you should explicitly state that you intend to hide/shadow the method by using the new syntax. Having the new keyword in the syntax makes it obvious to anyone reading your code that you are hiding a base implementation.


Note: It is just a warning, because by implementing the non-virtual method in your derived class you ARE hiding/shadowing the base class' implementation. The compiler is just telling you to be a good citizen and inform others that are reading your code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top