An example of polymorphism (the overridden version of the method in each derived class)
- Create a class hierarchy in which each specific shape class derives from a common base class.
- Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.
public class Shape
{
// A few example members
public int X { get; private set; }
public int Y { get; private set; }
public int Height { get; set; }
public int Width { get; set; }
// Virtual method
public virtual void Draw()
{
Console.WriteLine("Performing base class drawing tasks");
}
}
public class Circle : Shape
{
public override void Draw()
{
// Code to draw a circle...
Console.WriteLine("Drawing a circle");
base.Draw();
}
}
public class Rectangle : Shape
{
public override void Draw()
{
// Code to draw a rectangle...
Console.WriteLine("Drawing a rectangle");
base.Draw();
}
}
public class Triangle : Shape
{
public override void Draw()
{
// Code to draw a triangle...
Console.WriteLine("Drawing a triangle");
base.Draw();
}
}
// Polymorphism at work #1: a Rectangle, Triangle and Circle
// can all be used wherever a Shape is expected. No cast is
// required because an implicit conversion exists from a derived
// class to its base class.
var shapes = new List<Shape>
{
new Rectangle(),
new Triangle(),
new Circle()
};
// Polymorphism at work #2: the virtual method Draw is
// invoked on each of the derived classes, not the base class.
foreach (var shape in shapes)
{
shape.Draw();
}
/* Output:
Drawing a rectangle
Performing base class drawing tasks
Drawing a triangle
Performing base class drawing tasks
Drawing a circle
Performing base class drawing tasks
*/
Hide base class members with new members
If you want your derived class to have a member with the same name as a member in a base class, you can use the new keyword to hide the base class member.
public class BaseClass
{
public void DoWork() { WorkField++; }
public int WorkField;
public int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public new void DoWork() { WorkField++; }
public new int WorkField;
public new int WorkProperty
{
get { return 0; }
}
}
Prevent derived classes from overriding virtual members
A derived class can stop virtual inheritance by declaring an override as sealed. Stopping inheritance requires putting the sealed
keyword before the override
keyword in the class member declaration.
public class C : B
{
public sealed override void DoWork() { }
}
Sealed methods can be replaced by derived classes by using the new
keyword, as the following example shows:
public class D : C
{
public new void DoWork() { }
}
Note: It is recommended that virtual members use
base
to call the base class implementation of that member in their own implementation. Letting the base class behavior occur enables the derived class to concentrate on implementing behavior specific to the derived class. If the base class implementation is not called, it is up to the derived class to make their behavior compatible with the behavior of the base class.
Source: https://learn.microsoft.com/ru-ru/dotnet/csharp/fundamentals/object-oriented/polymorphism