One use of Interface – Polymorphism C#

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, Practically, these Human, Fish and Car 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.
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
             Benifit 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.




Ever wondered, declaring an interface and passing objects of type declared interface to functions? (Just like Javascript objects) Ex: Interface Rectangle { Length : number; Breadth : number; } Function FindArea( Rectangle rect): number { area : number; area = rect.Length * rect.Breadth; return area } Let rectangleOne = { Length: 50, Breadth : 20}; FindArea(rectangleOne); Is that Javascript? But wait how come OOPS? And that happens to be Typescript. The next big thing in client side development! BTW nice article :)

Like
Reply

To view or add a comment, sign in

More articles by Jatin Nagpal

Others also viewed

Explore content categories