Reputation: 10153
I am new to C# (coming from C++)
Can I declare a method to be virtual
and override
?(In C++ the method can be virtual in base and derived class as well)
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?
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.");
}
}
Upvotes: 0
Views: 458
Reputation: 2358
virtual
in base class, then it will continue
to remain virtual
in derived classes until it will not be marked
as sealed
. 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.
Upvotes: 2
Reputation: 3963
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)
Upvotes: 1
Reputation: 34844
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.
Upvotes: 0
Reputation: 3380
Yes, you can mark a method with virtual
keyword and then using override
override it in derived class.
new
to make your intention explicit and be sure that it's not a mistake.Upvotes: 0