Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

When should you use Abstract Class vs Interface while programming?

user-image
Question added by Ashfaque Choudhary , Programmer Analyst , Almarai
Date Posted: 2014/10/05

Interface:

 

An interface can contain methods, properties, events and indexers. 

Interface members must be implemented in your class.

 

In C#, Multiple inheritance support (we can inherit multiple interface in a class)

 

Abstract Class:

 

1. An abstract class is a class that cannot be instantiated, but must be inherited from. 

2. An abstract class may be fully implemented, but is more usually partially implemented or not       implemented at all,thereby encapsulating common functionality for inherited classes. 

3.  An Abstract Class can have abstract method and non abstract method. the abstract method must be override in derived class. 

4. An Abstract class always act as base class. 

5. An Abstract Class can inherit Interface. but an Interface Cannot.

 

Common Points:  

 

1. Both interfaces and abstract classes are useful for component interaction.

2. Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, 

they must all inherit from that class.

3. Abstract classes may also provide members that have already been implemented. 

Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.

 

Here are some Recommendations to help you to decide whether to use an interface or an abstract

class to provide polymorphism for your components :-

 

1.   If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

3.   If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

4.   If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

 

 

 

 

 

Jahanzeb Zaman
by Jahanzeb Zaman , Lead Solution Architect | Head of Development , Saudi Telecom Company

Interface is binding of contract and a way to force to implement all the methods if you want to use that interface type. Or if you want to inherit a class from more than one types.

Abstract class is used when a class wants to provide some of the functionality and let the others to use that functionality while extending some of it or providing new implementation of it (through overriding old and abstract methods). Java and C# don't support multiple inheritance of classes to avoid Diamond problem.

Sahar Kazi
by Sahar Kazi , Software Faculty , NIIT

Abstract Class should be used when you want to force the derived class to override only abstract methods.

Interface should be used when you want to force the class implementing interface to override all methods declared in interface.

use Abstract class only if its the only class to be inherited as C# doesn't supports multiple inheritance.

but you can implement multiple interface in a single class.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.