Click here to Skip to main content
12,297,650 members (23,975 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi everyone, I have a question that how can we implement the interfaces having same methodnames like this:
interface ISample2
{
    string CurrentTime();
    string CurrentTime(string name);
}
 
interface ISample1
{
    string CurrentTime();
}
I did like this:
class TwoInterfacesHavingSameMethodName : ISample1, ISample2
{
    static void Main(string[] sai)
    {
        ISample1 obj1 = new TwoInterfacesHavingSameMethodName();
        Console.Write(obj1.CurrentTime());
        ISample2 obj2 = new TwoInterfacesHavingSameMethodName();
        Console.Write(obj2.CurrentTime("SAI"));
        Console.ReadKey();
    }
 
    #region ISample1 Members

    string ISample1.CurrentTime()
    {
        return "Interface1:" + DateTime.Now.ToString();
    }
 
    #endregion

    #region ISample2 Members

    string ISample2.CurrentTime()
    {
        return "Interface2:FirstMethod" + DateTime.Now.ToString();
    }
 
    string ISample2.CurrentTime(string name)
    {
        return "Interface2:SecondMethod" + DateTime.Now.ToString() + "" + name;
    }
 
    #endregion
}
Here what is the meaning of this line:
ISample1 obj1 = new TwoInterfacesHavingSameMethodName();
Are we creating object for Class or Interface. What is the basic use of writing the methods in Interface?
Posted 26-Oct-10 2:48am
Edited 26-Oct-10 9:34am
Nish Nishant627.9K
v4
Comments
Yusuf 26-Oct-10 15:38pm
   
You can not instantiate Interface, so always you will create object for class.

In general, I'd try not to name methods between two interfaces the same thing. I know sometimes it may not be avoidable, but think about the readability of the code.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Ths basic use is to encapsule a related set of operations behind an interface, this interface can be common to more than one class. The idea is to reduce coupling between the consumer of the interface and implementing class.

In the example you gave you can access both interfaces from the same instance e.g.

TwoInterfacesHavingSameMethodName obj =  new TwoInterfacesHavingSameMethodName();
 
ISample1 sample1 = (ISample1)obj;
ISample2 sample2 = (ISample2)obj;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

[This article] explains interfaces in C#.

In your example below you are creating an instance of TwoInterfacesHavingSameMethodName, but it is implicitly converted because you're declaring it as ISample1.

ISample1 obj1 = new TwoInterfacesHavingSameMethodName();

An interface is like a contract. By implementing an interface you're making your class abide by the contract defined in the interface. Anything using that object instance doesn't need to know anything about the instance itself, just the interface it implements, ie. the defined contract.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

To add to the other answers, you only need to implement one method and both interfaces will be assumed to be implemented. Example:

interface IOne
{
    void Foo();
}
 
interface ITwo
{
    void Foo();
}
 
class Program : IOne, ITwo
{
    static void Main(string[] args)
    {
        IOne one = new Program();
        ITwo two = new Program();
        one.Foo();
        two.Foo();
    }
 
    public void Foo()
    {
        Console.WriteLine("Foo");
    }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can not instantiate an interface,however, u can create
its reference variabe.
You can just assign an object of a class (implementing the
interface) to the reference variable of the interface.
For more reference you should move for Google.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 26 Oct 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100