0

Actually the code below is Simple example of interface.But it showing an error 'PublicDemo.DemoPublic' does not implement interface member 'PublicDemo.demo1.two()'. 'PublicDemo.DemoPublic.two()' cannot implement an interface member because it is not public.

namespace PublicDemo
{
public interface demo
{
    void Demo();
}
public interface demo1:demo
{
    void one();
    void two();
}


class DemoPublic :demo1
{
    protected internal string variable;

   public void Demo()
    {
        Console.WriteLine("{0}", variable);

    }
    public void one()
    {
        Console.WriteLine("this is one method");
    }
    protected void two()
    {
        Console.WriteLine("this is two method");
    }


}


class Excute : DemoPublic
{
    static void Main(string[] args)
    {
        Excute Dp = new Excute();
        Dp.variable = Console.ReadLine();
        Dp.Demo();
        Dp.one();
        Dp.two();

        Console.ReadKey();

    }

}
}

i need why it is not working

1
  • Why is it protected Void Two()? Commented Jul 11, 2014 at 6:42

4 Answers 4

1

Change

protected void two()
{
    Console.WriteLine("this is two method");
}

into

public void two()
{
    Console.WriteLine("this is two method");
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Kireet, interface is a contract that means all implementations have to agree to implement public methods
one more doubt .is it possible to declare interface as protected
Yes, you can modify accessibility of interface itself, but not of its members. You can work around this using explicit interface implementation.
@Kireet If it is the nested interface you can make it protected, otherwise not.
can you give a simple example of multiple interfaces
|
1

You yourself answered the question:

'PublicDemo.DemoPublic.two()' cannot implement an interface member because it is not public.

Answer is Interface members have to be public.

Comments

0

It means what is says. Your two method is protected. It needs to be public if implemented from an interface.

protected void two(){

    Console.WriteLine("this is two method");
}

change it to public

Comments

0

Change Protected to Public.You have defined an public Interface.Once you define Public Interface,the Contracts in the Interface also will be public by Default.

public void two()
{
    Console.WriteLine("this is two method");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.