Professional Documents
Culture Documents
Interface Vs Abstract Class
Interface Vs Abstract Class
Interface Vs Abstract Class
This is a detailed analysis of Abstract classes and methods in C# with some concrete examples.
The keyword abstract can be used with both classes and methods in C# to declare them as
abstract.
The classes, which we can't initialize, are known as abstract classes. They provide only partial
implementations. But another class can inherit from an abstract class and can create their
instances.
For example, an abstract class with a non-abstract method.
using System;
class MyClient
{
public static void Main()
{
//MyAbs mb = new MyAbs();//not possible to create an instance
An abstract class can contain abstract and non-abstract methods. When a class inherits from an
abstract, the derived class must implement all the abstract methods declared in the base class.
An abstract method is a method without any method body. They are implicitly virtual in C#.
using System;
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.NonAbMethod();
mc.AbMethod();
}
}
But by declaring the derived class also abstract, we can avoid the implementation of all or certain
abstract methods. This is what is known as partial implementation of an abstract class.
using System;
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.AbMethod1();
mc.AbMethod2();
}
}
In C#, an abstract class can inherit from another non-abstract class. In addition to the methods it
inherited from the base class, it is possible to add new abstract and non-abstract methods as
showing below.
using System;
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
mc.AbMethod1();
}
}
An abstract class can also implement from an interface. In this case we must provide method
body for all methods it implemented from the interface.
using System;
interface IInterface
{
void Method1();
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
}
}
We can't use the keyword abstract along with sealed in C#, since a sealed class can't be abstract.
The abstract methods are implicitly virtual and hence they can't mark explicitly virtual in C#.
For example
using System;
class MyClient
{
public static void Main()
{
MyAbs ma1 = new MyClass1();// Polymorphism
ma1.AbMethod1();
ma1.AbMethod2();
}
}
NOTE: Abstract Class and Interface define a set of behavior for a class
Interface
In C# an interface definition looks like this:
Other Differences
Abstract Class Interface
Can have constructor. Can't have constructor.
Can only have abstract methods (implicitly abstract).
May or may not have abstract methods.
Abstract keyword is not allowed.
Methods can have access modifiers. No access modifier is allowed, implicitly public.
Can have fields. Not intended to define fields.
Cannot be directly instantiated. Cannot be directly instantiated.
Can be instantiated via sub-class. Can be instantiated via implementing class.
Can extend a Single class, implement Cannot extends a class, Can extend single or multiple
one or more interfaces. interfaces.
Can have virtual, static methods. virtual, static methods are not allowed.