Advertisement



< Prev
Next >



C# Polymorphism





Polymorphism means having many forms. In C#, polymorphism allows us to access an object in multiple ways, such as -
An object accessed in such multiple ways is said to be accessed polymorphically or polymorphic in nature.




Please Note:


All classes in C# are inherited from the Object class, which makes it a base class of all other classes. That's why a reference variable of Object class can be used to refer an object of any class.




Accessing an object polymorphically


  1. A reference variable can refer to any object of its own class and also an object of its derived class(in inheritance).


  2. //C# Example of Polymorphism 
    
    using System;
    
    //Base class
    class A
    {
    }
    
    
    //class B is a derived class of class A
    class B:A	
    {
    }
    
    class Polymorphism
    {
    public static void Main(String[] ar)
    {
    	//Object of B is assigned and will be accessed from a reference variable of its own B class.
    	B ob1 = new B();
    
    
    	//Object of B is assigned and will be accessed from a reference variable of its base class, A.	
    	A ob2 = new B();	
    
    
    	//Object of B is assigned and will be accessed from a reference variable of base class of all classes, Object.
    	Object ob3 = new B();	
    }
    }


    Program Analysis


      As you may see in the code above, an object of B class is accessed polymorphically by -
    • Reference variable of its own class B and,
    • Reference variable of its base class A and,
    • Reference variable of the base class of all classes, Object class.




  3. A reference variable declared as an interface type can refer to any object of the class that has implemented this interface.


  4. //C# Example of Polymorphism
    
    using System;
    
    
    //An interface
    interface A
    {
    }
    
    
    //class B is implementing interface A
    //Therefore, class B is derived class of A
    class B:A	
    {
    }
    
    
    class Polymorphism
    {
    public static void Main(String[] ar)
    {
    	//Object of B is assigned and will be accessed from a reference variable of its own B class.
    	B ob1 = new B();	
    
    
    	//Object of B is assigned and will be accessed from a reference variable of its base class, A.
    	//Which is an interface
    	A ob2 = new B();
    
    
    	//Object of B is assigned and will be accessed from a reference variable
    	//of the base class of all classes, Object.	
    	Object ob3 = new B();	
    }
    }


    Program Analysis


      As you may see in the code just above, the object of B class is accessed polymorphically by -
    • Reference variable of its own class B and,
    • Reference variable of its base class i.e. interface A and,
    • Reference variable of the base class of all classes, Object class.



    Advertisement




      Two types of polymorphism


    • Compile-time polymorphism.
    • Runtime polymorphism.




    • Compile-time polymorphism during method overloading.

    • Compile-time polymorphism takes place in the fulfilment of all of these conditions -
      • When a class inherits another class by inheritance.
      • Method of a base class is overloaded in its derived class.
      • An object of a derived class is accessed polymorphically(using the reference of its base class), to call on the overloaded method.


      //C# Compile-time polymorphism - method overloading
      
      
      using System;
      
      //Base class	
      class A				
      {
      //Defining the method Message()
      //This method takes an argument of its class type, A
      public void Message(A ob)
      {
      	Console.WriteLine("A");
      }
      }
      
      
      
      //class B is inheriting class A i.e. Derived class
      class B:A		
      {
      //Defining an overloaded version of method Message()
      //This version takes an argument of derived class type, B
      public void Message(B ob)       
      {
      	Console.WriteLine("B");
      }
      }
      
      class Poly
      {
      public static void Main(String[] ar)
      {
      	//ob1 is declared of type A but it is pointing to object of its derived class, B
      	A ob1 = new B(); 
      
      	//Calling the overloaded method Message()
      	//By using the object of derived class, B
      	//Which is referenced by the reference variable of its base class, A
      	//This method is passed the object of derived class, B	
      	ob1.Message(ob1);
      }
      }


      Output -


      A


      Program Analysis


      • Class A is inherited by a class B and its method Message() is overloaded in class B.
      • Reference variable ob1 is declared of type A but it is pointing to an object of its derived class, B.
      • Using the reference variable ob1, an overloaded version of method Message() is called, by passing the reference ob1 of type A.
      • Now, which overloaded version of method Message() is executed, is based on the type of reference variable, which is passed as an argument to call the Message() method, at the compile time.
      • Because, the ob1(reference of type A) is passed as an argument, so the version of method Message() of class A is called.



      Note:


      In compile-time polymorphism, which overloaded version of a method will be executed is decided at compile time, based on the type of reference variable passed to the overloaded method.





    • Runtime polymorphism during method overriding.

    Runtime polymorphism is also known as dynamic method dispatch during runtime. Runtime polymorphism takes place in fulfilment of these conditions -
    • When a class inherits another class and,
    • Method of the base class is overridden in its derived class.
    • An object of derived class is accessed polymorphically to call the overridden method.


    //C# Runtime polymorphism or dynamic method dispatch, during method overriding 
    
    
    using System;
    
    //Base class
    class Vegetable				
    {
    //Method Message() of base class Vegetable
    public virtual void Message()
    {
    	Console.WriteLine("Eat vegetables");
    }
    }
    
    
    //Derived class
    class Tomato:Vegetable		
    {
    //Overriding the method Message() of base class Vegetable
    public override void Message()			
    {
    	Console.WriteLine("Eat Tomatoes");
    }
    }
    
    
    
    class Polymorphism
    {
    public static void Main(String[] ar)
    {
    	Vegetable ob1= new Tomato();		//comment1
    	ob1.Message();				//comment2
    }
    }

    Output -


    Eat Tomatoes


    Program Analysis


    • A class Tomato is inherited from a class Vegetable and the method Message() of class Tomato is overridden.
    • At comment1, reference variable ob1(declared of base class type i.e. Vegetable), is pointing to an object of derived class i.e. Tomato class.
    • At comment2, the reference variables ob1 is calling the method Message(). But, which version Message() method is executed, is based on type of the object used to call the Message() at the runtime and not at the reference type, which is used to call Message() at compile time.
    • Hence, as ob1(a reference type Vegetable) is pointing to an object type Tomato, that's why at comment2, the method Message() of class Tomato is called.



    Note:


    In Runtime polymorphism, call to an overridden method is decided at runtime, based on the type of object used to call the method.



    Please share this article -




    < Prev
    Next >
    < C# Static Keyword
    C# Inheritance >



    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement