Reader Level:
ARTICLE

Interface in C#: Part 2

Posted by Sandeep Singh Shekhawat Articles | Articles C# January 07, 2013
In this article I am explaining that how can inherit an interface on class or interface. We will look here that how can inherit multiple interface on classes and use interface type variable.
  • 0
  • 0
  • 7340

in the previous article I explained how to declare and implement an interface.

Interface In C# - Part 1

In this article I am explaining how to inherit an interface by a class or interface. We will look here at how to inherit multiple interfaces on classes and use an interface type variable.

Example 1 

How can an interface inherit another interface?

We can inherit an interface by another interface. For example we have two interfaces, one is "IVehicle" and another is "ICart". The IVehicle interface is inherited by the ICart interface and the ICart interface is inherited by the "Vehicle" class. So we need to implement methods of both IVehicle and ICart interfaces in Vehicle classes.

using System;

namespace InterfaceExamples
{
     interface IVehicle
    {
        double Speed(int distance, int hours);
    } 

    interface ICart :IVehicle
   {
        double CalculateDistance(double speed,int hours);
    }
 
     class Vehicle : ICart
    {
        public double Speed(int distance, int hours)
        {
            double speed = 0.0d;
            speed = distance / hours;
            return speed;
        }
       public double CalculateDistance(double speed, int hours)
        {
            double distance = 0.0d;
            distance = speed * hours;
            return distance;
        }
    }   

    class Program
    {
        static void Main(string[] args)
        {
            int distance =500,hours=2;
            double speed,calDistance = 0.0; 
            Vehicle objVehicle = new Vehicle();
            speed = objVehicle.Speed(distance, hours);
            Console.WriteLine("speed is {0}", speed);
            calDistance = objVehicle.CalculateDistance(speed, hours);
            Console.WriteLine("calculated distance is {0}", distance);
            Console.Read();
        }       
    }
}

Interface-example1-in-Csharp.jpg

Example 2

Can a class inherit multiple interfaces?

Yes, a class or interface can inherit multiple interfaces. We have two interfaces "IVehicle" and "ICart". IVehicle has a "Speed()" method and ICart has a "CalculateDistance()" method. Both IVehicle and ICart interfaces are inherited by the "Vehicle" class so we implement methods of interfaces in the Vehicle class. If a class inherits more than one interface then each interface is separated by a comma (,).

using System;

namespace InterfaceExamples
{
     interface IVehicle
    {
        double Speed(int distance, int hours);
    } 
     interface ICart
    {
        double CalculateDistance(double speed,int hours);
    } 

    class Vehicle : IVehicle, ICart
    {
        public double Speed(int distance, int hours)
        {
            double speed = 0.0d;
            speed = distance / hours;
            return speed;
        }

       public double CalculateDistance(double speed, int hours)
        {
            double distance = 0.0d;
            distance = speed * hours;
            return distance;
        }
    }   

    class Program
    {
        static void Main(string[] args)
        {
            int distance =500,hours=2;
            double speed,calDistance = 0.0;
             Vehicle objVehicle = new Vehicle();
           speed = objVehicle.Speed(distance, hours);
            Console.WriteLine("speed is {0}", speed);
            calDistance = objVehicle.CalculateDistance(speed, hours);
            Console.WriteLine("calculated distance is {0}", distance);
            Console.Read();
        }       
    }
}

Interface-example2-in-Csharp.jpg

Example 3

Using an Interface to Declare a Variable

When we declare a variable of an interface type, we can assign it an object of any class that implements the interface. An interface type variable can call inherited members in a class but can't call all class public members but a class's object can call all its public members as well as inherited members.

We have an "IVehicle" interface that has a readonly "Speed" property and also has a derived class "Vehicle" that implements this interface. An IVehicle interface type variable created for the Vehicle class object and used property of its.

using System;

namespace InterfaceExamples
{
     interface IVehicle
    {       
        double Speed{get;}
    } 

    class Vehicle : IVehicle
   {
        double distance, time = 0.0; 
        public Vehicle(double distance, double time)
        {
            this.distance = distance;
            this.time = time;
        }      

        public double Speed
        {
             get
            {
                return distance / time;
            }
        }       
    }   

    class Program
    {
        static void Main(string[] args)
        {
            double distance, time = 0.0;
            Console.WriteLine("Enter Distance");
            distance = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter Time");
            time = Convert.ToDouble(Console.ReadLine());
            IVehicle vehicle = new Vehicle(distance,time);           
            Console.WriteLine("Speed is {0}", vehicle.Speed);
            Console.Read();
        }       
    }
}

Interface-example3-in-Csharp.jpg

Example 4

Remove ambiguity among Interfaces

When two interfaces have the same method/property name signature then we need to be explicit about the implementation of the methods. In that way when we implement the same method/property name from two interfaces on a single class; then we can't use public access specifiers for an inherited method/property in the class.

We have two interfaces "IVehicle" and "ICart". Both "IVehicle" and "ICart" interfaces have the same property "Message". These interfaces are inherited by the "Vehicle" class and uses interface type variables to access the "Message" property.

using System;

namespace InterfaceExamples
{
     interface IVehicle
    {       
        string Message {get;}
    }
     interface ICart
    {
        string  Message { get; }
    } 

    class Vehicle : IVehicle,ICart
    {  
        string  IVehicle.Message
        {
             get
            {
                return "It is a Vehicle Interface Message";
            }
        }
        string ICart.Message
        {
             get
            {
                return "It is a Cart Interface Message";
            }
        } 
    }
     class Program
    {
        static void Main(string[] args)
        {
            IVehicle vehicle = new Vehicle();
            Console.WriteLine("Message is : {0}", vehicle.Message); 
            ICart cart = new Vehicle();
            Console.WriteLine("Message is : {0}", cart.Message);
            Console.Read();
        }       
    }
}

Interface-example4-in-Csharp.jpg

COMMENT USING

Trending up