Table of contents
No headings in the article.
Abstract Class
Abstract class can be thought of as an incomplete class which can't be instantiated. When we say incomplete class what we mean is that the class contains methods which are not implemented in the base abstract class. Rather another class which inherits this class, implements that method.
Eg:
public abstract class Animal
{
public string Name {get; set; }
public abstract void Move();
}
Since every animal has different way of moving, the implementation of Move() function for animal class will be different.
Why Abstract class ?
Abstract class provides blueprint for all the derived classes. All the derived classes must implement the abstract definitions of the base abstract class.
An abstract method must be implemented in all non-abstract classes using the override keyword.
Let's take the above class as an example. Here Lion class inherits the Animal class and has its own implementation for the Move() function.
public class Lion : Animal
{
public Lion(string name)
{
this.Name = name;
}
public override void Move()
{
Console.WriteLine("Lion move");
}
}
One question which might arise is why do we need a class which can't be instantiated. From the implementation pov, we can get the same implementation using normal class and virtual method. Then why is abstract class needed in the first place ?
When we think about abstract class, we often forget that when derived class inherit the abstract class, they inherit its data members and other functions too. The derived classes can, hence share the common functionalities and have their own implementation for the abstract definitions.
We use an abstract class when we have some set of common functionalities to be shared between derived classes. If we were to use the above mentioned approach using normal class and virtual function, it may lead to code duplication for multiple classes.
Using abstract classes we can reduce this code duplication by just implementing the abstract method.
Interface
We know multiple inheritance is not possible in c#, meaning a class cannot have more than one base class. Let's take the animal class example to understand this.
So there are two type of animals, carnivorous and herbivorous. Let us have a separate class for each of them.
public class carnivorous
{
public void carniFood()
{
Console.WriteLine("I eat animals");
}
}
public class herbivorous
{
public void herbiFood()
{
Console.WriteLine("I eat plants");
}
}
public class Animal : carnivorous, herbivorous
{
public string name {get;set; }
public void Move()
{
Console.WriteLine("I move");
}
}
If we run this code, we will get error message "Class 'Animal cannot have multiple base classes: carnivorous and herbivorous.
So how do we solve this problem ?
Interfaces
An interface looks like a class, except it has no implementation. It only contains declaration of methods, properties (yes you read it right, properties too). Interfaces are inherited by classes or structs and that is where the declarations are implemented.
Every class that implements an interface must have implementation for all the interface member declared.
One would argue why are interfaces used when they don't implement functionality. We need to know that interfaces are used to define a contract between the classes and structs.
Also the above problem of C# not supporting multiple inheritance also gets solved with interfaces.
A class can implement as many interfaces as possible. So a solution to the above Animal Problem can be:
public Interface ICarnivorous
{
public void carniFood();
}
public Interface IHerbivorous
{
public void herbiFood();
}
public class Animal : ICarnivorous, IHerbivorous
{
public void carniFood()
{
Console.WriteLine("I eat animals");
}
public void herbiFood()
{
Console.WriteLine("I eat plants");
}
}
So now Animal class has functions implemented for both type of animals.
Abstract Class vs Interface
Abstract class contains both definition and implementation whereas Interface contains only declaration.
Abstract class can have constructor, static members whereas Interface can't have constructor, static members.
Abstract class can have all types of access modifiers whereas interface can only have public access modifier.
Multiple Inheritance is not achieved by abstract class whereas it is achievable by Interfaces.
A class can inherit from only one abstract class but implement multiple interfaces.
Conclusion:
With Abstract classes we can have methods implemented inside the class itself but in interface we can only define it, not implement it.
Class extends an abstract class whereas implements the interface.
Abstract classes can have implementation of some of their methods, unimplemented methods, or a combination of implemented and unimplemented methods. But, an interface can only have unimplemented methods (just the signature of methods). In other words, interfaces force you to have 100% abstraction, but using abstract classes we can control the amount of abstraction we want.
References : Wikipedia, articles, YouTube etc.
Cover picture reference : pngfind.com/mpng/mxRhoi_difference-between-..