1. Introduction
2. Pre-requisites
3. Roadmap

- Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
- Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
- Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
- Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
- Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
- Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
- Diving into OOP (Day 7): Properties in C# (A Practical Approach)
- Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
- Diving into OOP (Day 9): Understanding Events in C# (An Insight)
4. Runtime Polymorphism or Late Binding or Dynamic Binding
5. What are New and Override Keywords in C#?

InheritanceAndPolymorphism in your Visual Studio.ClassA
public class ClassA { public void AAA() { Console.WriteLine("ClassA AAA"); }
public void BBB()
{
Console.WriteLine(“ClassA BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassA CCC”);
}
}
ClassB
public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); }
public void BBB()
{
Console.WriteLine(“ClassB BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassB CCC”);
}
}
Program.cs
/// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) {
}
}
ClassA and ClassB have the same number of methods with similar names in both the classes. Now let’s inherit ClassA from ClassB, and create instances of the classes and call their methods in program.cs./// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); }
public void BBB()
{
Console.WriteLine(“ClassB BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassB CCC”);
}
}
/// <summary>
/// Class A, acting as a derived class
/// </summary>
public class ClassA : ClassB
{
public void AAA()
{
Console.WriteLine(“ClassA AAA”);
}
public void BBB()
{
Console.WriteLine(“ClassA BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassA CCC”);
}
}
Program.cs
/// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA x = new ClassA(); ClassB y=new ClassB(); ClassB z=new ClassA();
x.AAA(); x.BBB(); x.CCC();
y.AAA(); y.BBB();y.CCC();
z.AAA(); z.BBB(); z.CCC();
}
}
Output
ClassB AAA ClassB BBB ClassB CCC ClassA AAA ClassA BBB ClassA CCC ClassB AAA ClassB BBB ClassB CCC
Warnings
'InheritanceAndPolymorphism.ClassA.AAA()' hides inherited member 'InheritanceAndPolymorphism.ClassB.AAA()'. Use the new keyword if hiding was intended.
‘InheritanceAndPolymorphism.ClassA.BBB()’ hides inherited member
‘InheritanceAndPolymorphism.ClassB.BBB()’. Use the new keyword if hiding was intended.
‘InheritanceAndPolymorphism.ClassA.CCC()’ hides inherited member
‘InheritanceAndPolymorphism.ClassB.CCC()’. Use the new keyword if hiding was intended.
ClassB is the super class of class ClassA. That means ClassA is the derived class and ClassB is the base class. The class ClassA comprises ClassB and something more. So we can conclude that object of ClassA is bigger than object of ClassB. Since ClassA is inherited from ClassB, it contains its own methods and properties. Moreover, it will also contain methods/properties that are inherited from ClassB too.y. It looks like ClassB and initialized by creating an object that also looks like ClassB, well and good. Now, when we call the methods AAA and BBB and CCC through the object y, we know that it will call them from ClassB.ClassA, i.e., the derived class. It is initialized to an object that looks like ClassA. When we call AAA, BBB and CCC method through x, it calls AAA, BBB and CCC from ClassA.z again looks like ClassB, but it is now initialized to an object that looks like ClassA which does not give an error as explained earlier. But there is no change at all in the output we get and the behavior is identical to that of object y.Therefore initializing it to an object that looks like ClassB or ClassA does not seem to matter.6. Experiment
AAA and new behind BBB methods of ClassA, i.e., the derived class.ClassB
/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); }
public void BBB()
{
Console.WriteLine(“ClassB BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassB CCC”);
}
}
ClassA
/// <summary> /// Class A, acting as a derived class /// </summary> public class ClassA : ClassB { public override void AAA() { Console.WriteLine("ClassA AAA"); }
public new void BBB()
{
Console.WriteLine(“ClassA BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassA CCC”);
}
}
Program.cs
/// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB y = new ClassB(); ClassA x = new ClassA(); ClassB z = new ClassA();
y.AAA(); y.BBB(); y.CCC();
x.AAA(); x.BBB(); x.CCC();
z.AAA(); z.BBB(); z.CCC();
Console.ReadKey();
}
}
Error: 'InheritanceAndPolymorphism.ClassA.AAA()': cannot override inherited member 'InheritanceAndPolymorphism.ClassB.AAA()' because it is not marked virtual, abstract, or override
InheritanceAndPolymorphism: It’s the namespace I used for my console application, so you can ignore that.virtual, abstract or override in the base class.
/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public virtual void AAA() { Console.WriteLine("ClassB AAA"); }
public virtual void BBB()
{
Console.WriteLine(“ClassB BBB”);
}
public virtual void CCC()
{
Console.WriteLine(“ClassB CCC”);
}
}
/// <summary>
/// Class A, acting as a derived class
/// </summary>
public class ClassA : ClassB
{
public override void AAA()
{
Console.WriteLine(“ClassA AAA”);
}
public new void BBB()
{
Console.WriteLine(“ClassA BBB”);
}
public void CCC()
{
Console.WriteLine(“ClassA CCC”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB y = new ClassB();
ClassA x = new ClassA();
ClassB z = new ClassA();
y.AAA(); y.BBB(); y.CCC();
x.AAA(); x.BBB(); x.CCC();
z.AAA(); z.BBB(); z.CCC();
Console.ReadKey();
}
}
Output
ClassB AAA ClassB BBB ClassB CCC ClassA AAA ClassA BBB ClassA CCC ClassA AAA ClassB BBB ClassB CCC
z only and not in x and y. This strange output occurred only after we added virtual modifier in the base class methods. The difference is in the objectz. z looks like the base class ClassB but is initialized to an instance that looks like that of derived class ClassA. C# knows this fact. When we run z.AAA(), C# remembers that instance z was initialized by a ClassA object and hence it first goes to class ClassA, too obvious. Here the method has a modifier override which literally means, forget about the data type of z which is ClassB, call AAA from ClassA as it overrides the AAA of the base class. The override modifier is needed as the derived class methods will get first priority and be called upon.AAA of the base class ClassB. We are infact telling C# that this AAA is similar to the AAAof the one in base class.BBB as we see has the new modifier.z.BBB() calls BBB from ClassB and not ClassA. New means that the method BBB is a new method and it has absolutely nothing to do with the BBB in the base class. It may have the same name i.e. BBB as in the base class, but that is only a coincidence. As z looks like ClassB, the BBB of ClassB gets called even though there is a BBB inClassA. When we do not write any modifier, then it is assumed that we wrote new. So every time we write a method, C# assumes it has nothing to do with the base class.virtual method. Virtual means that the base class is granting us permission to invoke the method from the derived class and not the base class. But, we have to add the modifier override if our derived class method has to be called.7. Run Time Polymorphism with Three Classes
ClassC, and design our three classes and program.cs as follows:/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); }
public virtual void BBB()
{
Console.WriteLine(“ClassB BBB”);
}
public virtual void CCC()
{
Console.WriteLine(“ClassB CCC”);
}
}
/// <summary>
/// Class A, acting as a derived class
/// </summary>
public class ClassA : ClassB
{
public virtual void AAA()
{
Console.WriteLine(“ClassA AAA”);
}
public new void BBB()
{
Console.WriteLine(“ClassA BBB”);
}
public override void CCC()
{
Console.WriteLine(“ClassA CCC”);
}
}
/// <summary>
/// Class C, acting as a derived class
/// </summary>
public class ClassC : ClassA
{
public override void AAA()
{
Console.WriteLine(“ClassC AAA”);
}
public void CCC()
{
Console.WriteLine(“ClassC CCC”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB y = new ClassA();
ClassB x = new ClassC();
ClassA z = new ClassC();
y.AAA(); y.BBB(); y.CCC();
x.AAA(); x.BBB(); x.CCC();
z.AAA(); z.BBB(); z.CCC();
Console.ReadKey();
}
}
Output
ClassB AAA ClassB BBB ClassA CCC ClassB AAA ClassB BBB ClassA CCC ClassC AAA ClassA BBB ClassA CCC
virtual and the derived class used the modifieroverride, the derived class method will get called. Otherwise the base class method will get executed. Therefore forvirtual methods, the data type created is decided at run time only.static data type of the object.y that looks like of ClassB but is initialized here to the derived class, i.e., ClassA.y.AAA() first looks into the class ClassB. Here, it verifies whether the method AAA is marked virtual. The answer is an emphatic no and hence everything comes to halt and the method AAA gets called from class ClassB.y.BBB also does the same thing, but the method now is defined virtual in class ClassB. Thus C# looks at the classClassB, the one it was initialized to. Here BBB is marked with the modifier “new”. That means BBB is a new method which has nothing to do with the one in the base class. They only accidentally share the same name. So as there is no method called BBB (as it is a new BBB) in the derived class, the one from base class gets called. In the scene ofy.CCC(), the same above steps are followed again, but in the class ClassB, we see the modifier override, that by behaviour overrides the method in the base class. We are actually telling C# to call this method in class ClassA and not the one in the base class, i.e., ClassB.
x which also looks like that of class ClassB is now initialized with an object that looks like our newly introduced class ClassC and not ClassA like before. Since AAA is a non virtual method, it gets called from ClassB. In the case of method BBB, C# now looks into class ClassC. Here, it does not find a method named BBB and so ultimately propagates and now looks into class ClassA. Therefore the above rules repeat on and on and it gets called from class ClassB. In the case of x.CCC, in class ClassC, it is already marked new by default and therefore this method has nothing to do with the one declared in class ClassB. So the one from ClassC does not get called but the one from class ClassB where it is marked as override.CCC method in ClassC and change it to the code as shown below:public override void CCC() { Console.WriteLine("ClassC CCC"); }
new to override, the CCC of ClassC will now be called.z looks like that of ClassA but is now initialized to an object that looks like the derived class ClassC, we know we can do this. So z.AAA() when called, looks first into class ClassA where it is flagged as virtual. Do you recall that AAA is non virtual in class ClassB but marked as a virtual in ClassA. From now on, the method AAA is virtual in ClassC also but not in class ClassB. Virtual always flows from upwards to downwards like a waterfall. Since AAA() is marked virtual, we now look into class ClassC. Here it is marked override and thereforeAAA() gets called from class ClassC. In the case of BBB(), BBB() is marked virtual in class ClassB and new inClassA, but as there is no method BBB in ClassC, none of the modifier matters at all in this case. Finally it gets invoked from class ClassA. At last for method CCC, in class ClassC it is marked as new. Hence, it has no relation with the CCC in class ClassA which results in method CCC gets invoked from ClassA but not ClassB.internal class A { public virtual void X() { } }
internal class B : A
{
public new void X()
{
}
}
internal class C : B
{
public override void X()
{
}
}
Error: 'InheritanceAndPolymorphism.C.X()': cannot override inherited member 'InheritanceAndPolymorphism.B.X()' because it is not marked virtual, abstract, or override
X() in class B is marked new. That means it hides the X() of class A. If we talk about class C, B does not supply a method named X. The method X defined in class B has nothing to do with the method X defined in class A. This means that the method X of class B does not inherit the virtual modifier from the method X() of class A. This is what the compiler complained about. As the method X in B has no virtualmodifier, in C we cannot use the modifier override. We can, however, use the modifier new and remove the warning?8. Cut Off Relations
internal class A { public virtual void X() { Console.WriteLine("Class: A ; Method X"); } }
internal class B : A
{
public new virtual void X()
{
Console.WriteLine(“Class: B ; Method X”);
}
}
internal class C : B
{
public override void X()
{
Console.WriteLine(“Class: C ; Method X”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
A a = new C();
a.X();
B b = new C();
b.X();
Console.ReadKey();
}
}
Output
Class: A ; Method X Class: C ; Method X
X() in class C, we get:Output
Class: A ; Method X Class: B ; Method X

virtual methods. Sometimes, they are too confusing, the result is entirely different with what we expect. Object a looks like an A but is initialized to the derived class C. Since X is virtual, C# now goes and looks into class C. But before looking into the class C, it realizes that in class B, X is new. That’s it, this thing cuts of all connection with the X in A. Thus the keyword new is preceded with virtual, otherwise the override modifier would give us an error in class C. As X in class B is marked as new method, having nothing to do with the class A, class Cinherits a new which also has nothing to do with the class A. The X in class C is related to the X of class B and not of class A. Thus the X of class A gets invoked.b looks like class B now but in turn is initialized to an object of class C. C# first looks at classB. Here X is new and virtual both, which makes it a unique method X. Sadly, the X in C has the override modifier which sees to it that the X of C hides the X of B. This calls the X of C instead. If we remove the override modifier from X in class C, the default will be new, that cuts off the relation from the X of B. Thus, as it is, a new method, the Xof B gets invoked.virtual method cannot be marked by the modifiers static, abstract or override. A non virtual method is said to be invariant. This means that the same method gets called always, irrespective of whether one exists in the base class or derived class. In a virtual method, the run-time type of the object decides on which method to be called and not the compile-time type as is in the case of non virtual methods. For a virtual method, there exists a most derived implementation which always gets called.9. Runtime Polymorphism with Four Classes

ClassD. So we go more deep into the concept of Polymorphism and Inheritance.ClassD./// <summary> /// Class A /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } }
/// <summary>
/// ClassB
/// </summary>
public class ClassB:ClassA
{
public override void XXX()
{
Console.WriteLine(“ClassB XXX”);
}
}
/// <summary>
/// Class C
/// </summary>
public class ClassC : ClassB
{
public virtual new void XXX()
{
Console.WriteLine(“ClassC XXX”);
}
}
/// <summary>
/// Class D
/// </summary>
public class ClassD : ClassC
{
public override void XXX()
{
Console.WriteLine(“ClassD XXX”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA a = new ClassD();
ClassB b = new ClassD();
ClassC c=new ClassD();
ClassD d=new ClassD();
a.XXX();
b.XXX();
c.XXX();
d.XXX();
Console.ReadKey();
}
}
Output
ClassB XXX ClassB XXX ClassD XXX ClassD XXX
Explanation
virtual and override will be a bit complex.ClassB XXX, is the outcome of the statement a.XXX();. We have the method XXX marked virtual in class ClassA. Therefore, when using new keyword, we now proceed to class ClassB and not ClassD as explained earlier. Here, XXX has an override and since C# knows that class ClassC inherits this function XXX. In the classClassC, since it is marked as new, C# will now go back and not proceed further to class ClassD. Finally the methodXXX gets called from class ClassB as shown in the output above.XXX in class ClassC to override, then C# will proceed to class ClassD and call the XXX of class ClassD as it overrides the XXX of ClassC./// <summary> /// Class C /// </summary> public class ClassC : ClassB { public override void XXX() { Console.WriteLine("ClassC XXX"); } }
XXX in class ClassD and the method will get invoked from class ClassC as the default isnew.b, everything seems similar to object a, as it overrides the XXX of class ClassA.c here looks like ClassC. In class ClassC,XXX() is new and therefore it has no connection with the earlier XXX methods. In class ClassD, we actually override the XXX of class ClassC and so the XXX of ClassD gets invoked. Just remove the override and then it will get invoked from class ClassC. The object d does not follow any of the above protocols as both the sides of the equal sign are of same data types.new, static or virtual along withoverride. But abstract is permitted./// <summary> /// Class A /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } }
/// <summary>
/// ClassB
/// </summary>
public class ClassB:ClassA
{
public override void XXX()
{
base.XXX();
Console.WriteLine(“ClassB XXX”);
}
}
/// <summary>
/// Class C
/// </summary>
public class ClassC : ClassB
{
public override void XXX()
{
base.XXX();
Console.WriteLine(“ClassC XXX”);
}
}
/// <summary>
/// Class D
/// </summary>
public class ClassD : ClassC
{
public override void XXX()
{
Console.WriteLine(“ClassD XXX”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA a = new ClassB();
a.XXX();
ClassB b = new ClassC();
b.XXX();
Console.ReadKey();
}
}
Output
ClassA XXX ClassB XXX ClassA XXX ClassB XXX ClassC XXX
XXX is virtual or not, it will be treated as non virtual by the keyword base. Thus the base class XXX will always be called. The object aalready knows that XXX is virtual. When it reaches ClassB, it sees base.XXX() and hence it calls the XXX method ofClassA. But in the second case, it first goes to class ClassC, here it calls the base.XXX, i.e., the method XXX of classClassB, which in return invokes method XXX of class ClassA.10. The Infinite Loop
/// <summary> /// Class A /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } }
/// <summary>
/// ClassB
/// </summary>
public class ClassB:ClassA
{
public override void XXX()
{
((ClassA)this).XXX();
Console.WriteLine(“ClassB XXX”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA a = new ClassB();
a.XXX();
}
}
Output
ClassA, it will always call XXX from class ClassB and not ClassA. So we get no output.11. Summary
- In C#, a smaller object could be equated to a bigger object.
- The
overridemodifier is needed as the derived class methods will get first priority and be called upon. - These modifiers like
newandoverridecan only be used if the method in the base class is avirtualmethod.Virtualmeans that the base class is granting us permission to invoke the method from the derived class and not the base class. But, we have to add the modifier override if ourderivedclass method has to be called. - If the base class object declared the method
virtualand thederivedclass used the modifier override, thederivedclass method will get called. Otherwise, thebaseclass method will get executed. Therefore forvirtualmethods, the data type created is decided at run time only. - All the methods not marked with
virtualare non virtual, and the method to be called is decided at compile time, depending upon thestaticdata type of the object. - An
overridemethod is a method that has theoverridemodifier included on it. This introduces a new implementation of a method. We can’t use the modifiers such asnew,staticorvirtualalong withoverride. Butabstractis permitted.
12. Conclusion
Read more:
- C# and ASP.NET Questions (All in one)
- MVC Interview Questions
- C# and ASP.NET Interview Questions and Answers
- Web Services and Windows Services Interview Questions
Other Series
My other series of articles:
For more informative articles visit my Blog.


7 comments