Can any one please tell me what is the difference between Abstract classes and Interfaces in C# ?
Can any one please tell me what is the difference between Abstract classes and Interfaces in C# ?
An abstract class is a class that can not be instantiated but that can contain code.
An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.
If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.
You can implement multiple interfaces but only inherit from one class.
An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.
An interface can have neither of the two.
What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Example for Interface :
Code:public interface IVehicle { void Start(); void Drive(); void Park(); void ChangeGear(int gear); void SwitchOff(); } public class Vehicle : IVehicle { public void Start() { Console.WriteLine("The vehicle has been started"); } public void Drive() { Console.WriteLine("The vehicle is being driven"); } public void Park() { Console.WriteLine("The vehicle is being parked"); } public void ChangeGear(int gear) { Console.WriteLine("Gear changed to " + gear.ToString()); } public void SwitchOff() { Console.WriteLine("The vehicle has been switched off"); } }
Example for Abstract class :
Code:public abstract class Vehicle { public void Start() { Console.WriteLine("The vehicle has been started"); } public abstract void Drive(); public abstract void Park(); public abstract void ChangeGear(int gear); public void SwitchOff() { Console.WriteLine("The vehicle has been switched off"); } } public class Car : Vehicle { public Car() { } public override void Drive() { Console.WriteLine("The car is being driven"); } public override void Park() { Console.WriteLine("The car is being parked"); } public override void ChangeGear(int gear) { Console.WriteLine("The car changed gear changed to " + gear.ToString()); } }
Following are three fundamental difference between interfaces and abstract base :
- Abstract base classes can include code (methods).
- Abstract base classes can include data (fields).
- You can multiply-inherit from interfaces, but you cannot multiply-inherit from abstract base classes.
There is a basic difference that you can check. If you are working with library for external clients you have to use abstract classes for that purpose. This have advantages as they keep getting newer version. This gives you option to add new members in abstract class without manipulating the current code. Now if talk about the interface we need to manipulate the existing code to add new interface. The older version needs to be replaced for upgrades. So there are less chances that what you want to implement might not work for longer time.
Abstract class is better for implementing newer details. While interface lack this support. So here in one word if I say Interface classes are static and there are minor features that can be used for future manipulation the code. In comparison to that abstract classes needs the user library to be work on the base of other class. You can simply implement the interface on it. So in short there are lesser restrictions on it.
abstract class contain the internal member variables, and basic behavior in the form of methods that have been coded.
Bookmarks