Monday, 16 October 2017

One use of Interface – Polymorphism C#

We can implement the interface for those classes where behavior are same but definitions are different.
For example:

We are having 3 classes – Human, Fish and Car, all can move but way/ definition of moving is not identical.

Benefit
  • We are clearly able to group by the fact that they share common behavior
  • You need only single list to hold movable object, not 10 list of each type of object
  • We want to group these object by behavior they have in common. So do this, we use the interface

using System;
using System.Collections.Generic;
 
namespace ConsoleApplication4
{
    interface IMovable
    {
        void Move();
    }
 
    class Human : IMovable
    {
        public void Move() // Add code that defined what it called Human to move
        {
            Console.WriteLine("I am a Human, I move by Walking");
        }
    }
 
    class Fish : IMovable
    {
        public void Move()  // Add code that defined what it called Fish to move
        {
            Console.WriteLine("I am a Fish, I move by swimming");
        }
    }
 
    class Car : IMovable
    {
        public void Move() // Add code that defined what it called Car to move
        {
            Console.WriteLine("I am Car, I move By wheel");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            //User of Interface : Make a group of object by the fact they all move
            //Grouping by the behaviour not by the  class hierarchy
           
            /*
             We are clearly able to group by the fact that they share common behaviour
             Benefit here:
                 You need only single list to hold movable object, not 10 list of  each type of object
            */
            List<IMovable> lstMovable = new List<IMovable>
            {
                new Human(),   // Polymorphism in action [Different Form]: We are adding the Human object to the list designed of movable object 
                new Fish(),    // Polymorphism in action [Different Form]: We are adding the Fish object to the list designed of movable object
                new Car()      // Polymorphism in action [Different Form]: We are adding the Car object to the list designed of movable object
            };
 
            foreach(IMovable movableType in lstMovable)
            {
                 movableType.Move();
                // When movable type is Human, the method from the Human class is invoked
                // When movable type is Fish, the method from the Fish class is invoked
                // When movable type is Car, the method from the Car class is invoked
            }
 
            Console.ReadLine();
        }
    } 
Note: Why we can’t use Abstract Class here
  • Class like Fish, Human or Car – all of these objects can move but all are having different way to move or different definitions

Comment if you require more info....


How to Install/provision Dynamics Portal (SaaS) using Admin Center

Go to Office 365 Admin Center Go to https://portal.office.com/ Click on Admin Install Dynamics Portal for your CRM instance Clic...