5

I was reading this , and noted the second point in the question:

An another interviewer asked me what if you had a Public variable inside the interface, how would that be different than in Abstract Class? I insisted you can't have a public variable inside an interface. I didn't know what he wanted to hear but he wasn't satisfied either.

I read the answers and none of them seems to clarify this point, except this:

For .Net,

Your answer to The second interviewer is also the answer to the first one... Abstract classes can have implementation, AND state, interfaces cannot...

I think the answer to the interviewer was correct, as you cant have any variables inside the interface. I am a bit confused here. Can anybody clarify? My question is, why did the interviewer ask such a weird(?) question?

Community
  • 1
  • 1
Sharun
  • 3,022
  • 6
  • 30
  • 59

2 Answers2

8

All interface members are implicitly public, that is why you can't have public with properties or method in the interface.

interface C# - MSDN

Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.

For your question:

I think the answer to the interviewer was correct, as you cant have any variables inside the interface.

No. You can define properties in the interface. Something like:

interface ITest
{
    int MyProperty { get; set; }
}

public class TestClass : ITest
{
    public int MyProperty
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

EDIT:

An another interviewer asked me what if you had a Public variable inside the interface, how would that be different than in Abstract Class?

Probably the interviewer was trying to see if you would say that all members in interface are public by default, whereas in Abstract class you can have private, protected, public members etc.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks, but my question is, why did the interviewer ask such a question? Was that a weird question? – Sharun May 16 '13 at 04:39
  • @Sharun, I don't think so, difference between abstract class and interface is a commonly asked question, also what is the default access specifier for an interface. – Habib May 16 '13 at 04:40
  • I was asking about this part of the question: `An another interviewer asked me what if you had a Public variable inside the interface, how would that be different than in Abstract Class?` – Sharun May 16 '13 at 04:42
  • Ya, I know interfaces can contain properties. May be as someone commented to an answer to the original question, properties was " his weird way of saying `public variable` inside an interface" – Sharun May 16 '13 at 04:44
  • @Sharun, probably the interviewer was trying to see if you would say that all members in interface are public by default, whereas in Abstract class you can have private, protected, public members etc. But yes it is a weired question, but interviewers do all kind of stuff – Habib May 16 '13 at 04:44
  • Ya probably that was what in his mind when he asked that question. Anyway thank you for your help. :) Can you include your last comment to the answer so that I can accept it? – Sharun May 16 '13 at 04:48
  • @Habib: Access modifiers are not allowed in Interface. So when you write: interface ITest { public int MyProperty { get; set; } } will give you compiler error as The modifier 'public' is not valid for this item. – Santosh Panda May 16 '13 at 06:30
  • @skumar, yes that was a typo :) removed it. The reason access specifier is not allowed is because they are all `public` by default. – Habib May 16 '13 at 06:32
0

Just to add to Habib's answer, everything in an interface is public, because really it wouldn't make any sense for something to be private, since there can't be any implementation in it, the private member would never be used, it can't be used since there is nothing to use it.

I guess this is sort of a question where you need to elaborate on it a little bit more, it's not a bad question at all, I would say.

Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79