This document summarizes key concepts about polymorphism in C#, including method overloading, method overriding, and the base and sealed keywords. It explains that polymorphism allows the same method name to have different implementations. Method overloading uses the same name but different parameters at compile-time, while method overriding uses the same name and signature but different implementations at runtime through inheritance. The base keyword accesses members of the parent class, and sealed prevents a class or method from being overridden or inherited.
2. Polymorphism
• Polymorphism means many forms (ability to take more than one
form). In Polymorphism poly means “multiple” and morph means
“forms” so polymorphism means many forms.
• In polymorphism we will declare methods with same name and
different parameters in same class or methods with same name and
same parameters in different classes.
• Polymorphism has ability to provide different implementation of
methods that are implemented with same name.
• In Polymorphism we have 2 different types those are
• Compile Time Polymorphism (Called as Early Binding or Overloading
or static binding)
• Run Time Polymorphism (Called as Late Binding or Overriding or
dynamic binding)
3. Method Overloading
• Compile time polymorphism means we will declare
methods with same name but different signatures
because of this we will perform different tasks
with same method name.
• This compile time polymorphism also called as early
binding or method overloading.
• Method Overloading or compile time polymorphism
means same method names with different
signatures (different parameters)
4. • If we create two or more members having same name
but different in number or type of parameter, it is
known as member overloading. In C#, we can overload:
• methods, constructors, and indexed properties
• It is because these members have parameters only.
• Having two or more methods with same name but
different in parameters, is known as method overloading
in C#.
• The advantage of method overloading is that it
increases the readability of the program because you
don't need to use different names for same action.
• You can perform method overloading in C# by two ways:
• By changing number of arguments
• By changing data type of the arguments
5. Method Overloading example
• public class Class1
• { public void NumbersAdd(int a, int b)
• { Console.WriteLine(a + b);
• }
• public void NumbersAdd(int a, int b, int c)
• { Console.WriteLine(a + b + c);
• } }
• In above class we have two methods with same name but
having different input parameters this is called method
overloading or compile time polymorphism or early
binding.
6. Method Overridding
• Run time polymorphism also called as late
binding or method overriding or dynamic polymorphism.
• Run time polymorphism or method overriding means same
method names with same signatures.
• In this run time polymorphism or method overriding we
can override a method in base class by creating similar
function in derived class this can be achieved by using
inheritance principle and using “virtual & override”
keywords.
• In base class if we declare methods with virtual keyword
then only we can override those methods in derived class
using override keyword
7. • If derived class defines same method as defined in
its base class, it is known as method overriding in
C#.
• It is used to achieve runtime polymorphism. It
enables you to provide specific implementation of
the method which is already provided by its base
class.
• To perform method overriding in C#, you need to
use virtual keyword with base class method
and override keyword with derived class method.
8. Method Overridding
• //Base Class
• public class Bclass
• { public virtual void Sample1()
• { Console.WriteLine("Base Class");
• }}
• // Derived Class
• public class DClass : Bclass
• { public override void Sample1()
• {Console.WriteLine("Derived Class");
• } }
9. Method Overridding example
• // Using base and derived class
• class Program
• { static void Main(string[] args)
• { // calling the overriden method
• DClass objDc = new DClass();
• objDc.Sample1();
• // calling the base class method
• Bclass objBc = new DClass();
• objBc.Sample1(); } }
• Output:
• Derived Class
• Derived Class
10. • C# Base
• In C#, base keyword is used to access fields,
constructors and methods of base class.
• You can use base keyword within instance method,
constructor or instance property accessor only. You
can't use it inside the static method.
• C# base keyword: accessing base class field
• We can use the base keyword to access the fields
of the base class within derived class. It is useful if
base and derived classes have the same fields. If
derived class doesn't define same field, there is no
need to use base keyword. Base class field can be
11. • C# base keyword example: calling base class
method
• By the help of base keyword, we can call the base
class method also.
• It is useful if base and derived classes defines
same method. In other words, if method is
overridden.
• If derived class doesn't define same method, there
is no need to use base keyword. Base class method
can be directly called by the derived class method.
12. C# Sealed
• C# sealed keyword applies restrictions on the class
and method. If you create a sealed class, it cannot
be derived. If you create a sealed method, it cannot
be overridden.
13. For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials