An abstract class looks a like an interface, but the concept is a little confusing for the developer of OOP. An abstract class looks like an interface, of course, without any implementation, however they have their fair share of differences. While an abstract class can be either partially or fully implemented, an interface must be implemented completely. Well, the most notable difference between the two is that an abstract class can have default implementation, while an interface is just the definition of methods which contains only the member declarations. Let’s discuss both in detail.
An abstract class is a special type of class which acts as a base of other classes and cannot be instantiated. The implementation logic of an abstract class is provided by its derived classes. To make a class abstract, the “abstract” modifier is used which means some missing implementation needs to be implemented in the class derived from it. It contains both abstract and non-abstract members. An abstract class is intended to provide basic functionality which can be further shared and overridden by multiple derived classes. It is useful to avoid any kind of code duplication. They look very much like interfaces but with added functionality.
An interface, on the other hand, is not a class which contains only the signature of functionality. It’s a pattern with no implementation. Conceptually speaking, it’s just the definition of methods which contains only the declaration of members. It’s an empty shell which does not contain the implementation of its members. It’s like an abstract base class which only contains abstract members such as methods, events, indexers, properties, etc. It cannot be instantiated directly and its members can be implemented by any class. Additionally, multiple interfaces can be implemented by a class, however, a class can only inherit a single class.
Theoretically there are basically 5 differences between Abstract Class and Interface which are listed as below:
1. A class can implement any number of interfaces but a subclass can at most use only one abstract class.
2. An abstract class can have non-abstract Methods (concrete methods) while in case of Interface all the methods has to be abstract.
3. An abstract class can declare or use any variables while an interface is not allowed to do so.
4. An abstract class can have constructor declaration while an interface cannot do so.
5. An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface we cannot declare any access modifier(including public) as all the members of interface are implicitly public.
Use abstract classes when:
- A hierarchy of types that stem from universal base type exhibit similar behaviors or traits unique to only types of that hierarchy.
- Each type in the hierarchy has its own implementation of a generic behavior which itself should be marked as abstract in the base class.
Use an Interface when:
- The trait of behavior is not specific to any group of types or hierarchy but can be implemented by any type.
- The intention is to expose a particular trait or behavior to all types.
We have expertise in building web applications
, desktop applications, custom software development, enterprise portals, mobile applications, and digital solutions.
Our core skills covers web application development
with .NET core, ASP.NET MVC, C#, MEAN Stack, PHP, WordPress, IOS, Android, CSS3, HTML5, Bootstrap, SEO, Link Building.