An abstract class is a way to achieve abstraction in C#.
To declare an abstract class, we use the abstract keyword. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class definition. The Abstract classes are typically used to define a base class in the class hierarchy.
Example:
CSharp
using System;
public abstract class G {
public abstract void gfg1();
}
public class G1 : G {
public override void gfg1()
{
Console.WriteLine("Class name is G1");
}
}
public class G2 : G {
public override void gfg1()
{
Console.WriteLine("Class name is G2");
}
}
public class main_method {
public static void Main()
{
G obj;
obj = new G1();
obj.gfg1();
obj = new G2();
obj.gfg1();
}
}
|
Output :
Class name is G1
Class name is G2
Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly. Example:
CSharp
using System;
interface interface1 {
void show();
}
class MyClass : interface1 {
public void show()
{
Console.WriteLine("Welcome to GeeksforGeeks!!!");
}
public static void Main(String[] args)
{
MyClass obj1 = new MyClass();
obj1.show();
}
}
|
Output:
Welcome to GeeksforGeeks!!!
Difference between Abstract Class and Interface
Abstract Class | Interface |
---|
It contains both declaration and implementation parts. | It contains only the declaration of methods, properties, events, or indexers. Since C# 8, default implementations can also be included in interfaces. |
---|
Multiple inheritance is not achieved by abstract class. | Multiple inheritance is achieved by interface. |
---|
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 contains methods, properties, indexers, events. |
---|
The keyword “:” can be used for implementing the Abstract class. | The keyword “:” and “,” can be used for implementing the Interface. |
---|
It can be fully, partially or not implemented. | It should be fully implemented. |
---|
To declare abstract class , we use abstract keyword. | To declare interface, we use interface keyword. |
---|
Example of Abstract class:- public abstract class Fruits{ public abstract void Mango(); } | Example of Interface:- public interface Readable{ void read(); } |
---|