Answer to Question #178958 in C# for muhammad arfan

Question #178958

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


1
Expert's answer
2021-04-11T05:17:28-0400
namespace Q178958
{
    // 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