An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.
Defining an Interface
interface interfaceName
{
}
Differences between Interfaces and Abstract classes
*An Interface cannot implement methods.*An abstract class can implement methods.
*An Interface can only inherit from another Interface.
*An abstract class can inherit from a class and one or more interfaces.
*An Interface cannot contain fields.
*An abstract class can contain fields.
*An Interface can contain property definitions.
*An abstract class can implement a property.
*An Interface cannot contain constructors or destructors.
*An abstract class can contain constructors or destructors.
*An Interface can be inherited from by structures.
*An abstract class cannot be inherited from by structures.
*An Interface can support multiple inheritance.
*An abstract class cannot support multiple inheritance.
Example
using System;
namespace InterfaceConsoleApplication
{
class Program : myInterface //Implement the interface
{
static void Main(string[] args)
{
Program p1 = new Program();
p1.display(); //call the method
}
//Give funtionality to method who declared in interface
public void display()
{
Console.WriteLine("Interface Method");
}
}
//declare interface
interface myInterface
{
void display();
}
}
Output :-
Interface Method
In this example, I have created a interface myInterface using interface keyword and define a method named display().Class Program inherits the myInterface and give full definition to method display() and call by the Program class object p1.
Leave Comment