Difference between class and interface in C#?

What is difference between class and interface in C# and another object oriented programming languages?


Views: 50167 | Community Opinion: 22

Tags..  OOPS Concept  C#  Class. Interface  ASP.Net interview  Object Oriented Programming

Bookmark this page..



Ask a New Question Go to Home

Community Opinion/Answers
 
Sumit Said..

A C# Class Considered being the primary building block of the language. What I mean by the primary building block of the language is that every time you work with C# you will create Classes to form a program. We use Classes as a template to put the properties and functionalities or behaviors in one building block for some group of objects and after that we use that template to create the objects we need.

A class can contain declarations of the following members:

Constructors, Destructors, Constants, Fields, Methods, Properties,Indexers, Operators, Events, Delegates, Classes, Interfaces, Structs



An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

An interface can inherit from one or more base interfaces. When a base type list contains a base class and interfaces, the base class must come first in the list.



interface ISampleInterface
{
void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}

static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();

// Call the member.
obj.SampleMethod();
}
}




Suruchi Kapoor Said..

Can U cite a simple example for it?




Mehul Said..

we can also use like this......

ImplementationClass obj = new ImplementationClass
(); // Call the member. ((ISampleInterface)obj).SampleMethod();




vcg Said..

vc




Faez Said..

If you can provide Examples, with real time examples. Its is much more easer to understand...
Thank you!!!!




Zeel Said..

if u want to understand abt it thn its really usefull n also 4 understand d example
thnx a lot....!!!!




Mayur Said..

what is main benefit of the interface???




SAJ Said..

the main benefit of the INTERFACE is enabling multiple INHERITANCE IN c Sharp and mostly used in EVENT DRIVEN PROGRAMMING





these all the answers are theoretical
Can u tell me difference between IDatareader and Datareader




Refat Said..

thank you




Rajesh Said..

C# does not support the Multiple Inheritance(Using in C++).To Implement the multiple inheritance concept in C# and Java we use the interface.Interface always declared with public Keyword.
we can not create the object of interface but we can create the virtual object of interface.
In interface we can only declare the method/function but not define here.we must define it in that class who inherit the interface. for examples

public interface A
{
void show();
}
public interface B
{
void disp();
}
class C:A,B
{
public void show()
{

}
public void disp()
{
}
}
class test
{
public static void Main()
{
C c1=new C();
c1.show();
c2.disp();
A a1=c1;//Virtual Object
Console.ReadLine();
}
}




formulaone87 Said..

Hi rajesh...r other frndz:-)

kindly let me knw the use of creating virtual objects for interface??? will be needy...

anyone else are welcome too... plz..




samm Said..

so objects of interface can be made in c# unlike in java?? I thought in java, interface objects cannot be made because they dont have a constructor. That virtual object thing is what i dont get.




aaa Said..

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

An interface can inherit from one or more base interfaces. When a base type list contains a base class and interfaces, the base class must come first in the list.




DhanaLakshmi Said..

Interface :-


An Interface is an type of Class. But, This class should not Contain any implementation in it. Interface can give the reference(Object) to all other classes to perform the function.

When we have to use the interface?

Answer :-
When the application Contains n number of classes that time we can use interface. because through the interface we can access all the classes and methods in the application....


Abstract Class :-

An Abstract class is a class which will contains both definition and implementation in it.... But, only thing is that we can't create instance(Object) to the Abstract class if you create then you will error.... Once if you define the Methods in Abstract class you should implement it in the same class or otherwise you can use "Override" Keyword to implement in different Phase(Classes).


When we have to use the interface?

Answer :-

Whenever we should want to implement the method in our application we can create or define the methods in abstract class..... The important code will be Maintain in the Abstract class only......




Dhanalakshmi Said..

Serialization :-

The Serialization is nothing but Convert an Object into a form or Stream..... and then it will store the values in DB,Asp.Net Cache or Physical File....
and whenever we want we can retrieve them this process is called as the De-serialization..... This will only use for Memory Stream..




akash Said..

IN C# with in the interface we can declare main() or not? can U giv a simple example plzzz................................................................................................................................................................................................................................................................................




Bharti Said..

Below are some very good link for answer of above questions and many more of oops concept www.interviewqsn.com/what-is-difference-between-abstract-class-and-an-interface.html

www.getproductprice.com/SubCategory-class.aspx

www.getproductprice.com/Default.aspx

www.interviewqsn.com




bharti Said..

Below are some important links will helps you to under stand difference between c# and java and some more questions on java and csharp
www.getproductprice.com/Ads-Features-of-csharp-over-Java.aspx
www.interviewqsn.com/why-csharp-is-better-then-java-vb-and-c-plus-plus.html




Prasanna Said..

Can't we create method signature in Class other than in Interface?Please let me know the answer.......................................................................................................................................................................................................................................................................................................................................................................




Kokare Rajendra (Oxford Info.) Said..

mostly benifit of this concept avidation of code as well as maintane security

Interface :-


An Interface is an type of Class. But, This class should not Contain any implementation in it. Interface can give the reference(Object) to all other classes to perform the function.

When we have to use the interface?

Answer :-
When the application Contains n number of classes that time we can use interface. because through the interface we can access all the classes and methods in the application....


Abstract Class :-

An Abstract class is a class which will contains both definition and implementation in it.... But, only thing is that we can't create instance(Object) to the Abstract class if you create then you will error.... Once if you define the Methods in Abstract class you should implement it in the same class or otherwise you can use "Override" Keyword to implement in different Phase(Classes).


When we have to use the interface?

Answer :-

Whenever we should want to implement the method in our application we can create or define the methods in abstract class..... The important code will be Maintain in the Abstract class only......




manoj kumar Said..

interface can not contain constructor,fields and so on.


in class contain cons,field and does not support multipul inheritance.

ex:
interface testmk
{
public testmk()
{
}
void getmethod();
string getname();
}



after compile it gives error :-

interface can not contain constructor.






What do you think? Add your opinion/answer
*Name
*your opinion/answer: