-4

What is an Interface in c#?

I know that Interface is an Entity not a class. But some of the blog i have visited suggested that Interface is a class. If Interface is a class then it should support Data abstraction.

Please clarify me what is Interface in details.

I visited this site for preparing interview questions:

33. What is difference between Class And Interface?

Class is logical representation of object. It is collection of data and related sub procedures with defination.

Interface is also a class containg methods which is not having any definations.Class does not support multiple inheritance. But interface can support.

If interviwer ask me what is interface what should i told him/her? And when they ask it support Data Abstraction or not??

Community
  • 1
  • 1
  • 4
    http://msdn.microsoft.com/en-us/library/ms173156.aspx – nsgocev Aug 08 '14 at 13:15
  • http://theprofessionalspoint.blogspot.in/2013/10/125-basic-c-interview-questions-and.html Question number 33. Read the answer – Saurav Kundu Aug 08 '14 at 13:20
  • An interface is not a class. An interface defines what a class that inherits from it must implement. – L-Four Aug 08 '14 at 13:33
  • The answer to question 33 is wrong (for C# at least). Iinterface is defined with the `interface` keyword, and a class is defined by `class`. They have completely different meanings and purposes and you can't just take and compare. – Lukas G Aug 08 '14 at 13:34
  • 1
    Well, just as I finished my answer, it gets put on hold... – Nzall Aug 08 '14 at 13:35
  • @NateKerkhofs make a comment, please, I'm very curious to read. – Lukas G Aug 08 '14 at 13:38
  • Basically, an interface is like a word template: you don't use it directly, but instead use it as a basis for the classes you're going to work with. It's not the same as a class, it's more like a barebone class template. You can define them to have a framework that makes classes with a similar purpose (like converters) adhere to a certain pattern (like a Convert() and a ConvertBack() method), so you can easily exchange one of them for another without having to rewrite large amounts of code. – Nzall Aug 08 '14 at 13:42
  • 1
    The question whether an interface is a class is a bad one, because it does not show any knowledge about the platform. If an interviewer asks you that, you should reply "An interface is a template that you use to define a class structure, but can't be used without having a class. They're 2 closely related but different objects.". – Nzall Aug 08 '14 at 13:45
  • Thanks all of you for giving your time. Another clarification needed please .. Did interface support Data Abstraction?? – Saurav Kundu Aug 08 '14 at 13:45
  • An interface IS an abstraction: you abstract away the details of the implementation to provide a bird's eye view of the concept you're implementing. – Nzall Aug 08 '14 at 13:46
  • @NateKerkhofs I was confused to see that Interface is a class. I want a proper definition.. And hopefully i got it. – Saurav Kundu Aug 08 '14 at 13:47
  • Thanks all of you. have a great time – Saurav Kundu Aug 08 '14 at 13:48

2 Answers2

1

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.

Source: http://www.csharp-station.com/Tutorial/CSharp/lesson13

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Gustobg
  • 11
  • 1
  • here is also confusion.. Struct dont support inheritance.. So how they can inherit Interface?? – Saurav Kundu Aug 08 '14 at 13:26
  • here is the further details...http://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance – Gustobg Aug 08 '14 at 13:29
  • 1
    "which must provide an implementation for each interface member declared": not true. An interface method can be declared as abstract in an abstract class that implements the interface, so it does not have to have an implementation. – L-Four Aug 08 '14 at 13:38
  • Please can you write a small example then it will be very helpful – Saurav Kundu Aug 08 '14 at 13:42
  • @user3922384: I have no clue what you just said. – L-Four Aug 08 '14 at 13:43
  • Did u check the link I provided?? I think you should go and check those examples first. – Gustobg Aug 08 '14 at 13:52
  • Yes I did, I still don't understand what you mean. Are you saying I'm wrong? – L-Four Aug 08 '14 at 13:56
  • No L-Three...my previous comment was for Saurav Kundu and previous to previous comment for you. Yes you are correct. But helping this poor guys in bits and pieces you should provide him some real help. Bcos that happens with me to when I use to ask trivial questions here...and people waste their time by commenting unwanted or deleting question. – Gustobg Aug 08 '14 at 14:16
  • 1
    Hi, @user3922384, and welcome to Stack Overflow! You seem to have copied the text of your answer from http://www.csharp-station.com/Tutorial/CSharp/lesson13, or one of the many sites that copied it from there. Your intentions are good, I'm sure, but this falls foul of [Stack Overflow's requirements for referencing other material](http://stackoverflow.com/help/referencing). Notice that I've added an attribution line to your answer. Please do the same in future answers that contain content written by others. Thanks! – Michael Petrotta Aug 08 '14 at 23:32
0

I've actually asked this question in interviews before, so I have a good idea of what the interviewer is probably looking for in an answer. The article you cited states,

Interface is also a class containg methods which is not having any definations.Class does not support multiple inheritance. But interface can support.

Ignoring the grammatical and spelling problems here, an interface is not a class. The two are significantly different concepts. If an interviewee were to give me this answer, I would think they had a pretty poor understanding of the subject (or at least that they were very bad at communicating it).

A much better way of answering this kind of interview question would be:

An interface is a type which may contain event, method, property, or indexer declarations, but not their definitions. Classes do not support multiple inheritance, but interfaces do. Also unlike classes or structs, interfaces may not define constructors or be directly instantiated; rather, an interface may be implemented by a class or struct, which may then be instantiated.

You might also point out that unlike classes, generic type parameters in interfaces may be declared as covariant or contravariant.

Interfaces are just one of the many ways programmers use abstraction (abstract classes are another). They allow you to specify the general contract a class (or struct) must satisfy, without knowing anything about how it satisfies that contract.

Now regarding your confusion about entities… The term 'entity', at least within the context of .NET applications, generally refers to classes declared in an Entity Framework model, which is used for interacting with databases via code. Read more about Entity Framework:

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331