Polymorphism is often referred to as the third pillar of object-oriented programming, ~ encapsulation and also inheritance. Polymorphism is a Greek word that means "many-shaped" and also it has two distinct aspects:

At run time, objects that a derived class may it is in treated as objects of a base class in locations such as technique parameters and collections or arrays. When this polymorphism occurs, the object"s declared kind is no longer the same to its run-time type.

You are watching: A derived class always ____ case or instance of the more general base class.

Virtual methods enable you to work with groups of associated objects in a uniform way. For example, suppose you have actually a drawing application that permits a user to produce various type of shapes on a drawing surface. You don"t understand at compile time i beg your pardon specific types of forms the user will certainly create. However, the application has to keep monitor of every the various types of forms that are created, and also it has to update them in an answer to user computer mouse actions. You can use polymorphism to solve this trouble in two basic steps:

Create a class hierarchy in i m sorry each particular shape course derives indigenous a typical base class.Use a virtual method to invoke the appropriate method on any derived course through a solitary call to the base course method.

First, produce a base course called Shape, and also derived classes such as Rectangle, Circle, and Triangle. Provide the Shape class a virtual an approach called Draw, and override it in each derived class to draw the particular shape the the course represents. Create a perform object and include a Circle, Triangle, and Rectangle come it.

public course Shape // A few example members public int X get; exclusive set; windy int Y get; personal set; public int elevation get; set; public int width get; set; // Virtual technique public virtual void Draw() Console.WriteLine("Performing base class drawing tasks"); public course Circle : Shape publicly override void Draw() // code to draw a circle... Console.WriteLine("Drawing a circle"); base.Draw(); public course Rectangle : Shape windy override void Draw() // code to draw a rectangle... Console.WriteLine("Drawing a rectangle"); base.Draw(); public course Triangle : Shape publicly override void Draw() // password to draw a triangle... Console.WriteLine("Drawing a triangle"); base.Draw(); To upgrade the illustration surface, usage a foreach loop to iterate through the list and also call the Draw method on each shape object in the list. Even though every object in the list has a declared type of Shape, it"s the run-time type (the overridden variation of the method in each obtained class) that will certainly be invoked.

// Polymorphism at work-related #1: a Rectangle, Triangle and Circle// can all be supplied whereever a shape is expected. No cast is// required since an implicitly conversion exists from a derived// class to its base class.var forms = brand-new List brand-new Rectangle(), brand-new Triangle(), brand-new Circle();// Polymorphism at work-related #2: the virtual method Draw is// invoked on every of the obtained classes, not the base class.foreach (var shape in shapes) shape.Draw();/* Output: illustration a rectangle Performing base class drawing tasks illustration a triangle Performing base course drawing tasks drawing a one Performing base course drawing tasks*/In C#, every form is polymorphic because all types, consisting of user-defined types, inherit native Object.

Polymorphism overview

Virtual members

When a derived class inherits native a base class, it gains all the methods, fields, properties, and also events that the base class. The designer the the derived class has different selections for the habits of online methods:

The derived course may override digital members in the basic class, defining brand-new behavior.The derived course inherit the closest base class method without overriding it, keeping the existing habits but enabling further acquired classes to override the method.The derived class may define new non-virtual implementation that those members that hide the base course implementations.

A derived course can override a base course member just if the base course member is claimed as online or abstract. The acquired member have to use the override keyword to clearly indicate the the method is to plan to take part in online invocation. The complying with code provides an example:

public class BaseClass public digital void DoWork() public virtual int WorkProperty gain return 0; public class DerivedClass : BaseClass public override void DoWork() public override int WorkProperty acquire return 0; Fields can"t be virtual; only methods, properties, events, and indexers deserve to be virtual. When a derived course overrides a online member, that member is dubbed even when an circumstances of that course is being accessed as an instance of the base class. The adhering to code offers an example:

DerivedClass B = brand-new DerivedClass();B.DoWork(); // phone call the new method.BaseClass A = B;A.DoWork(); // also calls the brand-new method.Virtual methods and properties enable derived great to expand a base class without needing to use the base course implementation that a method. For an ext information, watch Versioning with the Override and brand-new Keywords. An interface offers another means to specify a method or set of techniques whose implementation is left to acquired classes.

Hide base class members with new members

If you want your derived class to have a member with the exact same name as a member in a basic class, you have the right to use the new keyword to hide the base class member. The brand-new keyword is put prior to the return form of a course member the is gift replaced. The complying with code gives an example:

public class BaseClass public void DoWork() WorkField++; public int WorkField; publicly int WorkProperty acquire return 0; public course DerivedClass : BaseClass public brand-new void DoWork() WorkField++; public new int WorkField; public new int WorkProperty get return 0; Hidden base course members might be accessed from customer code by casting the instance of the derived course to an circumstances of the basic class. For example:

DerivedClass B = new DerivedClass();B.DoWork(); // phone call the brand-new method.BaseClass A = (BaseClass)B;A.DoWork(); // calls the old method.

Prevent obtained classes native overriding virtual members

Virtual members remain virtual, nevertheless of how many classes have been declared between the online member and also the course that originally claimed it. If class A declares a digital member, and also class B derives indigenous A, and class C derives native B, class C inherits the virtual member, and also may override it, regardless of whether course B asserted an override for the member. The following code gives an example:

public class A public online void DoWork() public course B : A windy override void DoWork() A derived class can avoid virtual inheritance by declaring an override together sealed. Protecting against inheritance requires placing the sealed keyword before the override keyword in the class member declaration. The complying with code provides an example:

public class C : B publicly sealed override void DoWork() In the previous example, the an approach DoWork is no longer virtual to any class obtained from C. It"s still digital for instances of C, even if they"re cast to form B or type A. Sealed methods have the right to be changed by acquired classes by making use of the new keyword, as the following example shows:

public course D : C public new void DoWork() In this case, if DoWork is dubbed on D using a change of form D, the new DoWork is called. If a change of kind C, B, or A is offered to access an instance of D, a speak to to DoWork will follow the rule of digital inheritance, routing those calls to the implementation the DoWork on course C.

See more: Windows Git " Warning: Lf Will Be Replaced By Crlf, Lf Will Be Replaced By Crlf In Git

Access base course virtual members from acquired classes

A derived class that has replaced or overridden a method or property can still access the technique or home on the base course using the basic keyword. The adhering to code offers an example:

public course Base public virtual void DoWork() /*...*/ public class Derived : Base windy override void DoWork() //Perform Derived"s work here //... // call DoWork ~ above base course base.DoWork(); For an ext information, watch base.