Difference Between Abstract Class and Interfaces
In this short article we will discuss the difference between the Abstract class and the interface, through an example. many of us get confused with the difference between the abstract class and the interface, so let us know the difference of the two through definition only.
An Abstract class is a class which cannot be instantiated. Then you may ask that why would we need that class which cannot be instantiated. The answer of this is that we need a class to be abstract so that other class can inherit it. The advantages of doing this is that it maintains a hierarchical structure.
For Example think of a Bird class, many other class can inherit this bird class. So we make this class as a abstract so that any abstract methods define in this class can be overridden by the child class.public abstract class Bird
{
private string color;
public abstract string FoodHabit();
}
Now we cannot define FoodHabit of the bird in bird class since many bird have different food habits.So any child class which will inherit this class have to override this method and write their own FoodHabit.
An Interface is not a class, but we can say that it's an entity that is defined by the word Interface. Mainly interface is used to support multiple inheritence as we all know that in C# multiple inheritence cannot be done, as we cannot inherit more than one class. But using inheritence we can inherit more than one interfaces. Its the main advantage of interface over abstract class.
For Example, take the Bird's hirarchy only...public interface Vehicles
{
void FoodHabit();
}
One more difference between the abstract class and the interface is that interface cannot contain fields where as abstract class can contain fields as well as methods.
Thank to providing a wonderful different between Abstract Class and Interfaces . With the help of the resource ,we can easily understand