Differences between Abstract class and Interface in C#
What is an Abstract Class?
An abstract
class is a special type of class that cannot be instantiated. An abstract class
is designed to be inherited by subclasses that either implement or override its
methods. In other words, abstract classes are either partially implemented or
not implemented at all. You can have functionality in your abstract class—the
methods in an abstract class can be both abstract and concrete. An abstract
class can have constructors—this is one major difference between an abstract
class and an interface. You can take advantage of abstract classes to design
components and specify some level of common functionality that must be
implemented by derived classes.
Abstract class syntax
//Syntax of Abstract Class
public abstract class AbstractClass
{
}
Sample Code that demonstrate the use
of Abstract Class
// C#
program to illustrate the
// concept of
abstract class
// abstract
class 'CSharp'
public abstract class CSharp
{
// abstract
method 'CSharpMethod()'
public abstract void
CSharpMethod();
}
// class
'CSharpNaija1' inherit
// in child
class 'CSharp'
public class CSharpNaija1 : CSharp
{
// abstract
method 'CSharpMethod()'
// declare here
with
// 'override'
keyword
public override void
CSharpMethod()
{
Console.WriteLine("Class name is CSharpNaija1");
}
}
// class
'CSharp' inherit in
// another
child class 'CSharpNaija2'
public class CSharpNaija2 : CSharp
{
// same as the
previous class
public override void
CSharpMethod()
{
Console.WriteLine("Class name is CSharpNaija2");
}
}
// Driver Class
public class main_method
{
// Main Method
public static void Main()
{
// 'obj'
is object of class 'CSharp' class '
//
CSharp' cannot be instantiate
CSharp obj;
//
instantiate class 'CSharpNaija1'
obj = new CSharpNaija1();
// call
'CSharpMethod()' of class 'CSharpNaija1'
obj.CSharpMethod();
//
instantiate class 'CSharpNaija2'
obj = new CSharpNaija2();
// call
'CSharpMethod' of class 'CSharpNaija2'
obj.CSharpMethod();
}
}
What is an Interface?
An interface is basically a
contract—it doesn’t have any implementation. An interface can contain only method
declarations; it cannot contain method definitions. Nor can you have any member
data in an interface. Whereas an abstract class may contain method definitions,
fields, and constructors, an interface may only have declarations of events,
methods, and properties. Methods declared in an interface must be implemented
by the classes that implement the interface. Note that a class can implement
more than one interface but extend only one class. The class that implements
the interface should implement all its members. Like an abstract class, an
interface cannot be instantiated.
Interface syntax
//Interface Syntax
public interface ICsharpnaijaInterface
{
void CSharpMethod();
}
Sample Code that demonstrate the use
of Abstract Class
// C# program to illustrate the
// concept of
interface
//using System;
// A simple
interface
interface interface1
{
// method
having only declaration
// not
definition
void show();
}
// A class that
implements the interface.
class MyClass : interface1
{
// providing
the body part of function
public void show()
{
Console.WriteLine("Welcome to Csharp Naija!!!");
}
// Main Method
public static void Main(String[] args)
{
//
Creating object
MyClass obj1 = new MyClass();
// calling
method
obj1.show();
}
}
Difference between Interface and Abstract Class
Abstract Class
|
Interface
|
Multiple inheritance is not achieved by abstract class.
|
Multiple inheritance is achieved by interface.
|
It contains both declaration and definition part.
|
It contains only a declaration part.
|
It contain constructor.
|
It does not contain constructor.
|
It can contain static members.
|
It does not contain static members.
|
It can contain different types of access modifiers like
public, private, protected etc.
|
It only contains public access modifier because everything
in the interface is public.
|
The performance of an abstract class is fast.
|
The performance of interface is slow because it requires
time to search actual method in the corresponding class.
|
It is used to implement the core identity of class.
|
It is used to implement peripheral abilities of class.
|
A class can only use one abstract class.
|
A class can use multiple interface.
|
If many implementations are of the same kind and use common
behavior, then it is superior to use abstract class.
|
If many implementations only share methods, then it is
superior to use Interface.
|
Abstract class can contain methods, fields, constants, etc.
|
Interface can only contain methods.
|
It can be fully, partially or not implemented.
|
It should be fully implemented.
|
These both C# Interface vs
Abstract Class are great object-oriented programming concepts that are used
highly in developing applications as per the requirement. It is purely selected
the by the technical leads with which they are more comfortable and the
business requirement. These both C# Interface vs Abstract Class are easy
to use and simple to learn in any programming language.
When to Use Interface or Abstract Class in our application
In C#, an Abstract class or
interface are been
used for data abstraction. An interface is better than abstract
class when multiple classes need to implement the interface. The member of the
interface cannot be static. The only complete member of an abstract class can
be static.
C# does not support multiple
inheritances, interfaces are mainly used to implement the multiple
inheritances. As a class can implement more than one interface and only inherit
from one abstract class. An interface is mainly used only when we do not
require the implementation of methods or functionalities. An abstract
class is used when we do require at least
default implementation.
Points
to Remember
1. Interface
and Abstract classes cannot be instantiated
2. A
Class can inherit more than one interface but only one abstract class
3. Interface
cannot have member implementation but abstract may have one or more.
You can read more about abstract class and interface here
Comments
Post a Comment