Ad Code

Responsive Advertisement

POLYMORPHISM

POLYMORPHISM:- 

  1. Entities behaving in different ways depending upon the input it receives is known as polymorphism  i.e. whenever the input changes the output or behavior of entity also changes, this can be implemented with an approach known as-

  •         overloading
  •       overriding

        a.)     Overloading –

      It is an approach which allows defining multiple methods with the same name with in a class, but signature of the method should be different. Signature in the sense we can either change the type  of parameter being pass to the method or number of parameter being pass to the method or order of parameter being pass to the method.
public void show()
public void show(int x)
public void show(string s)
public void show(int x , string s)
public void show(string s, int x)

Note – Change in the return type of method is not taken into consideration in overloading.
-Add a class LoadDemo.cs and write the following:
    class LoadDemo
    {
        public void show()
        {
            Console.WriteLine(1);
       
        }
        public void show(int x)
        {
            Console.WriteLine(2);

        }
        public void show(string s)
        {
            Console.WriteLine(3);

        }
        public void show(int x, string s)
        {
            Console.WriteLine(4);

        }
        public void show(string s, int x)
        {
            Console.WriteLine(5);

        }
        static void Main()
        {
            LoadDemo d = new LoadDemo();
            d.show(); d.show(10); d.show("Hello");
            d.show(10, "hello"); d.show("hello", 10);
            Console.ReadLine();
        }
    }


          As discussed earlier whenever you changed the type of input applied to a method automatically a different method get executed that matches with the given type performing a different action.
          Overloading is an approach which allows you to provide multiple behaviors to a  method.
    example – the WriteLine method that is defined with 19 different behaviors which can print any type of value i.e. supplies to it.

Constructor Overloading –

          Just like we can overload methods in the same way constructors of a class also can be overloaded, when a class contain multiple constructor in it we can use any constructor to create object of that class.
-Add a class LoadCon.cs and write the following:
using System;

namespace oopsProject
{
    class LoadCons
    {
        int x;
        public LoadCons()
        {
            this.x = x;
        }
     
       public LoadCons(int x)
       {
           x = 100;
       }
               
        public void Display()
        {
            Console.WriteLine();
        }
       static void Main()
        {
            LoadCons c1 = new LoadCons();
            LoadCons c2 = new LoadCons(100);
            c1.Display(); c2.Display();

            Console.ReadLine();
        }
    }

}



We overload constructors of a class so that the variables of class can be initialized with either default values or by a given value supplied to the constructor as in our above example.

Inheritance Based overloading –

              If at all a method which is declared under parent class is overloaded in the child class we called it as inheritance based overloading.
-Add a class LoadParent.cs and write the following:
using System;

namespace oopsProject
{
    class LoadParent
    {
        public void show()
        {
            Console.WriteLine("Method Show1");
        }
      
        public void test1(int x)
        {
            Console.WriteLine("method test1");
        }   
    }
}




-Add a class Class1.cs and write the following:
    class LoadChild : LoadParent
    {
        public void test2(string s)
        {
            Console.WriteLine("Method test2");
        }
        static void Main()
        {
            LoadChild c = new LoadChild();
            c.show(); c.test1(10); c.test2("hello");
            Console.ReadLine();
        }
   
    }

Method overriding –

              If at all a parent class method is redefined under the child class with the same signature we call it as method overriding.
Differences between overloading and overriding
overloading
overriding
         it’s an approach of defining multiple methods with same name changing their signature
        It’s an approach of defining multiple methods with same name same signature.
         this can be done with in class as well as within the child class also
         This can be done only on the child class.
        To overload a parent class method under child we don’t required any perdition from parent.
         To override a parent class method under child we required an explicit permission from parent.

Reactions

Post a Comment

0 Comments