An interface has no implementation; it only has the signature or in other
words, just the definition of the methods without the body. the interface is not a
class that contains only the signature of functionality. It’s a pattern with
no implementation. Conceptually speaking, it’s just the definition of methods
that contains only the declaration of members. It’s an empty shell that does
not contain the implementation of its members. It’s like an abstract base class
which only contains abstract members such as methods, events, indexers,
properties, etc.
Syntax of Interface:-
|
interface
{
//Interface
member
}
|
Example of Interface in c#:-
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceExampleNamespace
{
interface MyfirstInterface
{
void myFirstInterfaceMethod();
}
class InterfaceExampleClass : MyfirstInterface
{
public static void Main()
{
InterfaceExampleClass cls
= new InterfaceExampleClass();
cls.myFirstInterfaceMethod();
}
public void myFirstInterfaceMethod()
{
Console.WriteLine("welcome
to dotnetTpoint , hi this is nitin kumar");
Console.ReadLine();
}
}
}
|
Abstract Class
An abstract class is a special type
of class that acts as a base of other classes and cannot be instantiated. The
implementation logic of an abstract class is provided by its derived classes.
To make a class abstract, the “abstract” modifier is used which means some
missing implementation needs to be implemented in the class derived from it. It
contains both abstract and non-abstract members.
Example of Abstract Class:-
|
using System;
namespace AbstractClassExampleNamespace
{
abstract class AbstractClassExampleClass
{
public void methodwithDefination()
{
Console.WriteLine("methodwithDefination
Method: This is the method of Abstract Class with Defination");
}
public abstract void methodWithoutDefination();
}
class TestingClass : AbstractClassExampleClass
{
public override void methodWithoutDefination()
{
Console.WriteLine("methodWithoutDefination
method: this is second method of interface without defination inside
interface ");
}
public void TestingClassOwnMethod()
{
Console.WriteLine("TestingClassOwnMethod
Method: This is another method of testing class");
}
}
class Program
{
static void Main(string[]
args)
{
TestingClass objTestingCls
= new TestingClass();
objTestingCls.methodwithDefination();
objTestingCls.methodWithoutDefination();
objTestingCls.TestingClassOwnMethod();
Console.ReadKey();
}
}
}
|
Difference between interface and
Abstract Class in c#:-
|
1. An abstract class cannot support
multiple inheritance, but an interface can support multiple
inheritance. Thus a class may inherit several interfaces but only one
abstract class.
2.
An interface is an empty shell, just only the signatures of the
methods. The methods do not contain anything. The interface can't do
anything. It's just a pattern. An Abstract class is a class which will
contains both definition and implementation in it.
3.
Abstract classes can have consts, members, method stubs and defined
methods, whereas interfaces can only have consts and methods.
4.
Various access modifiers such as abstract, protected, internal, public,
virtual, etc. are useful in abstract Classes but all methods of an interface
must be defined as public.
|