0

I have a few questions regarding interfaces.

  1. Why we can't use virtual keyword with Interfaces members

  2. Why we can't use override keyword in derived class from interfaces

  3. Suppose

    interface Iface
    {
        void Func();
    }
    class Program : Iface
    {
        static void Main(string[] args)
        {
    
        }
        public void Func()
        {
            Console.WriteLine("In func");
        }
    }
    

    Why I need to use public on member functions in derived class from interface i.e. in Func() definition? If I am not using public keyword it will result in a compile time error

  4. Can we use static Members in Interface?

J. Steen
  • 14,900
  • 15
  • 57
  • 62
Saurabh Mahajan
  • 2,753
  • 5
  • 22
  • 30

3 Answers3

5
  1. Marking a method virtual gives the inheriting class the option to override the respective method. But when inheriting from an interface the implementation is not optional but mandatory. Every interface method is abstract by definition.

  2. You don't override the methods, you implement them. An interface method has no own implementation, there is nothing to override. It just wouldn't make any sense.

  3. Why a C# interface method implemented in a class must be public

  4. No we can't use static methods on an interface

Community
  • 1
  • 1
Dennis Traub
  • 46,924
  • 7
  • 81
  • 102
2
  1. Interfaces don't need that. The implementation can be virtual.
  2. An implementation doesn't need that - there is nothing to override.
  3. You will have to instantiate a Program to call Func. Also there is no concept of static interfaces.
Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
  • I have fixed the wonky numbered list in OP's question - please reconsider your own numbering to match. =) – J. Steen Apr 19 '13 at 10:53
-1

Interfaces dont act like class because, we cant make an object of interfaces if we can make make an object of interfaces to achieve multiple inheritance then we can face diamond problem that occur in the case of multiple inheritance of classes.