0% found this document useful (0 votes)
70 views11 pages

Static or Compile Time Polymorphism

The document discusses two types of polymorphism: static/compile-time polymorphism and dynamic/runtime polymorphism. Static polymorphism is determined at compile-time through method overloading where the compiler selects the method based on parameters. Dynamic polymorphism is determined at runtime through method overriding where the runtime type selects the method. Virtual methods allow for dynamic polymorphism by enabling derived classes to override base class methods.

Uploaded by

Pratik Gandhi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views11 pages

Static or Compile Time Polymorphism

The document discusses two types of polymorphism: static/compile-time polymorphism and dynamic/runtime polymorphism. Static polymorphism is determined at compile-time through method overloading where the compiler selects the method based on parameters. Dynamic polymorphism is determined at runtime through method overriding where the runtime type selects the method. Virtual methods allow for dynamic polymorphism by enabling derived classes to override base class methods.

Uploaded by

Pratik Gandhi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

There are two types of polymorphism: 1. Static or compile time polymorphism 2.

Dynamic or runtime polymorphism

Static or Compile Time Polymorphism In static polymorphism, the decision is made at compile time.

Which method is to be called is decided at compile-time only. Method overloading is an example of this. Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call. Method overloading is a concept where a class can have more than one method with the same name and different parameters. Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.

Sealed Keyword Sealed keyword can be used to stop method overriding in a derived classes. By default, all methods are sealed, which means you can't override them, so that "sealed" keyword is redundant in this case and compiler will show you an error when you'll try to make sealed already sealedmethod. But if your method was marked as virtual in a base class, by overriding and marking this method with "sealed" will prevent method overriding in derived classes.

Virtual Keyword According to MSDN, The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. Virtual Method Virtual method is a method whose behavior can be overridden in derived class. Virtual method allows declare a method in base class that can be redefined in each derived class. When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

By default, methods are non-virtual. You cannot override a non-virtual method. You cannot use the virtual modifier with the static, abstract, private or override modifiers. Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax. It is an error to use the virtual modifier on a static property. A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier. Virtual method solves the following problem: In OOP, when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behavior is ambiguous. In C#, polymorphism is explicit - you must have a virtual (or abstract) modifier on the base class method (member) and an override on the derived class method, which you probably already know. If you don't put a modifier on a base class method, polymorphism can't ever happen. If you then add a non-modified method to the derived class with the same signature as the non-modified base class method, the compiler will generate a Warning message.

Polymorphism : May 11, 2011


Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance. It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name.

public class Customer { public virtual void CustomerType() { Console.WriteLine("I am a customer"); } } public class CorporateCustomer : Customer { public override void CustomerType() { Console.WriteLine("I am a corporate customer"); } } public class PersonalCustomer : Customer { public override void CustomerType() { Console.WriteLine("I am a personal customer"); } } public class MainClass { public static void Main() { Customer[] C = new Customer[3]; C[0] = new CorporateCustomer(); C[1] = new PersonalCustomer(); C[2] = new Customer(); foreach (Customer CustomerObject in C)

{ CustomerObject.CustomerType(); } } } Output: I am a corporate customer I am a personal customer I am a customer

Method Overloading ( Compile Time Polymorphism): Method with same name but with different arguments is called method overloading. Method Overloading forms compile-time polymorphism. Example of Method Overloading: class A1 { void hello() { Console.WriteLine("Hello"); } void hello(string s) { Console.WriteLine("Hello {0}", s); } }

Method Overriding (Run Time Polymorphism) : Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass. Method overriding forms Run-time polymorphism. Note: By default functions are not virtual in C# and so you need to write virtual explicitly. While by default in Java each function are virtual. Example of Method Overriding: class parent { virtual void hello() { Console.WriteLine("Hello from Parent"); } } class child : parent { override void hello() { Console.WriteLine("Hello from Child"); } } static void main() { parent objParent = new child(); objParent.hello(); } Output: Hello from Child.

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) Compile Time Polymorphism 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) 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. Run Time Polymorphism 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 Example

//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"); } }

// 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(); } } If we run above code we will get output like as shown below Output

---------------------------------Derived Class Derived Class

You might also like