In this article I have explained the difference between an Abstract Class and an Interface with some examples and some information about an Interface.
INTRODUCTION :
In this article I am going to explain the difference between an Abstract Class and an Interface with some examples.
DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE:
A) Abstract Class : We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.
Take a look at an Abstract class example:
The Code window looks like this:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication4{ abstract class M1 { public int add(int a, int b) { return (a + b); } } class M2 :M1 { public int mul(int a, int b) { return a * b; } } class test { static void Main(string[] args) { M2 ob = new M2(); int result = ob.add(10, 20); Console.WriteLine("the result is {0}", result); Console.ReadLine(); } }}When we run it the output looks like this:
B) An Interface : The syntax of an Interface looks like this:
interface{ //Interface member}
NOTE :
Take a look in an interface example:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication3{ interface MyInterface { void myMethod(); } class MyClass : MyInterface { public static void Main() { MyClass cls = new MyClass(); cls.myMethod(); } public void myMethod() { Console.WriteLine("welcome to MCN IT SOLUTION"); Console.ReadLine(); } }}After running this program we obtain output as follows:
NOTE : In C# , an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.
Programming Dictionary in C#