Abstract class vs Interface in C#.
1. An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
2. Using Abstract we can not complete multiple inheritance but using an Interface we can achieve multiple inheritance.
3. We can not declare a member field in an Interface .
4. Interface provide a public access modifies By default.
5. We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
6. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
7. An abstract class cannot defines it method it contain only declaration part.
Example:
Abstract Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationAbs
{
abstract class H1
{
public int add(int a, int b)
{
return (a + b);
}
}
class H2 : H1
{
public int min(int a, int b)
{
return a - b;
}
}
class test
{
static void Main(string[] args)
{
H2 ob = new H2();
int result = ob.add(190, 20);
Console.WriteLine("Abstract result is= {0}", result);
Console.ReadLine();
}
}
}
Output:
2. Using Abstract we can not complete multiple inheritance but using an Interface we can achieve multiple inheritance.
3. We can not declare a member field in an Interface .
4. Interface provide a public access modifies By default.
5. We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
6. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
7. An abstract class cannot defines it method it contain only declaration part.
Example:
Abstract Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationAbs
{
abstract class H1
{
public int add(int a, int b)
{
return (a + b);
}
}
class H2 : H1
{
public int min(int a, int b)
{
return a - b;
}
}
class test
{
static void Main(string[] args)
{
H2 ob = new H2();
int result = ob.add(190, 20);
Console.WriteLine("Abstract result is= {0}", result);
Console.ReadLine();
}
}
}
Output:


0 Comments