Skip to content

Method in interface

George George

Asked Oct 26, 2008, 11:21 PM EDT5 answers
Share
LinkedInX

Hello everyone,


I understand in the rule of C#, we are not allowed to add public/private/protected qualifier to methods in interface. My question is why adding the qualifiers are not allowed? I want to know the reason behind the claimed rule, i.e. what issues will be if we are allowed to add qualifier?


thanks in advance,
George

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    +1
    It's Because the interface members have to be public inorder to be accessed by all classes those need the interface services. In fact they are marked as public under the hood
    if they are marked as private so they could'nt be viewed ouside the interface scoop on one hand
    in the other hand, if they are marked as protected will do not make sens as the interface contains only methods contracts and not implementations going to be modified   
    +1
  • +1

    Thanks.

    1.

    I can understand that you mean if interface method is marked as private, then the implementation must be private and the implementation could not be accessed from outside? Is that your point?

    2.

    My confusion is what do you mean "if they are marked as protected will do not make sens as the interface contains only methods contracts and not implementations going to be modified"? Could you show some pseudo code to describe your ideas please?

    regards,

    George

    +1
  • Bechir Bejaoui
    +1
    1. For the first question, I think yes, but if you have another opinion please feel free to discuss this with me

    2. For the second question, I thing if you have to use protected keyword then abstract class is the best solution. To be more clear, the interface concept is used only for contracts context and not for implementation context


     
    +1
  • +1

    Can you show me how to make a method of an interface private please? I am confused and I tried seems we can not.

     

    regards,

    George

    +1
  • Bechir Bejaoui
    +1
    I think for the first question you miss understood me you said if interface method is marked as private, but in reality it couldn't be as it is designed to play the role of a contract  that provides a sort of comment and shared characterestics between different classes issued from different types in almost cases. This situation imposes that interfaces memebers have to be only public

    private: only visible inside a unique class
    protected: only visible inside the class and  the derived types
    public: visible by different types
    +1