Answer to Question #178892 in C# for muhammad arfan

Question #178892

How it is easy to implement and add interchangeable components using interface? Explain with example



1
Expert's answer
2021-04-09T13:16:47-0400
using System;


namespace Q178892
{
    // Creation of the interface of movement
    interface IMovable
    {
        void Move();
    }


    // Using an interface in a class Person
    class Person : IMovable
    {
        public void Move()
        {
            Console.WriteLine("The man is walks");
        }
    }
    // Using an interface in a class Car
    class Car : IMovable
    {
        public void Move()
        {
            Console.WriteLine("The car is driving");
        }
    }
    // Using an interface in a class Plane
    class Plane : IMovable
    {
        public void Move()
        {
            Console.WriteLine("The plane is flying");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            // Instantiating сlass оbjects
            Person person = new Person();
            Car car = new Car();
            Plane plane = new Plane();


            // Method call
            person.Move();
            car.Move();
            plane.Move();


            // Pause
            Console.Read();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS