Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Whats the difference between an abstract class and interface? When would you want to use them?

Generally speaking, for a company looking to install a new network, WiFi is usually the better option if you can afford it. It's easier to use and maintain, offers universal access and security for all users, and puts you on a solid upgrade path towards future unified communications upgrades.

Wired networks still have their place, but WiFi is the better option for creating a future-focused network that's aiming to be more accessible and usable as time goes on.

user-image
Question added by Rizwan Ashraf , System Analyst & Designer , SoftSolution Pvt. Ltd.
Date Posted: 2016/05/25

Abstract classInterface1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor. 5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 7) Example: public abstract class Shape{public abstract void draw();} Example: public interface Drawable{void draw();}

Abhijeet Saxena
by Abhijeet Saxena , Software Engineering Analyst , Accenture Services Pvt. Ltd

Keeping the difference part short and simple , I'll emphasis when to use Interface over Abstract class . (in terms of C#)Differences :

-Abstract class methods can have implementations, whereas Interface methods cannot.

 - Interface methods cannot be static or virtual or abstract.- Interface members cannot be private / protected etc.

All are public.

-No fields can be defined in Interface , in abstract class we can.

A Real time scenario to prefer Interface over abstract class:Suppose , we have existing signature(either by abstract class or interface) of functionality .Our Abstract class is serving the purpose of being the base class of several child classes, and those child classes are being used by different clients. The abstract class code is being used for commercial purpose and not supposed to be modified. In such scenario , if a new requirement comes , which requires signature(methods) of existing abstract class as well as some new methods ,to be used in some new child classes ; we'll face the drawback as multiple inheritance is not allowed in C#.

In such scenario, Interface would have been a better option instead of abstract class ,  to extend the functionality without affecting the current implementation.

 public Interface INewInterface : IExistingInterface{

//define new functionalities here

//utilise the existing functionalities 

}

Chinta Anusha
by Chinta Anusha , Hr Executive and Recruiter , Goadrige soft Solutions

  • we can not inherit multiple abstract classes in a single class.
  • Abstract class is a collection of abstract methods and non abstract methods.
  • Interfaces are used to implements multiple inheritance.
  • we can not create object to the interface.
  • we can't define a variable in interface.
  • To implement interface methods in derived class no need to use override keyword.

Karim Mansour
by Karim Mansour , Software Engineer , MedStreaming

both cannot create an instance of them

Interface is like a contract so it has only declaration but no implementation and all methods are public

while abstract class may have method declaration and implementation and they may either be private, protected or public

I use interfaces where I can make all dependencies implement the contract methods

while I use abstract classes when I have concrete implementation of methods that all child classes follow the same implementation

aamir zargar
by aamir zargar , network support engineer , rooman technologies Srinagar net tech Pvt LTD

Interfaces can't have a default implementation for any method. Abstract classes can. A class can implement any interface, but if you already have a class with a base type, it will not be capable of inheriting from an abstract class. When to use each one? In general I use both. I create an interface so all methods can be reimplemented in many different ways (that helps in remoting, for example, or to create fake implementations for testing purposes). But if some method is probably going to be implemented in the same way by many classes, I create an abstract class that already implement such method but let other methods be fully virtual.

Mohammad Nikravesh
by Mohammad Nikravesh , Software Developer , Dotin

There are many discussion on this topic. But the quick answer is the Interface apply behavior on class but abstract apply structure on a class

  1. An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
  2. Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
  3. We can not declare a member field in an Interface.
  4. We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
  5. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.

Igor Polischuk
by Igor Polischuk , Software Engineer , EPAM

Interface describes what class, which is inherited from this interface, has to implement. Interface can't include implementation.

Abstract class can execute the same functionality what interface does, also abstract class can include implementation.

Some class can be inherited from more then one interface, but at the same time, it can't be inherited from more then one class. This is true for C#, Java and Object Pascal (Delphi). I can't say it about other languages because I deal with these three languages only.

Hamad Hassan
by Hamad Hassan , IT Manager , Traffic Media

in abstract class some of the fuctions implemented in the class and some of them declared only and implemented in the class who inherite this class.

in interface no fuction is implemented in the class it's implement in the class who inherite this class

Mohanad Amen
by Mohanad Amen , Senior Web Developer , Sakhr

  • A class can implement any number of interfaces but a subclass can at most use only one abstract class.
  • An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods has to be abstract.
  • An abstract class can declare or use any variables while an interface is not allowed to do so.
  • In an abstract class all data member or functions are private by default while in interface all are public, we can’t change them manually.
  • In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don’t need to use that.
  • An abstract class can’t be used for multiple inheritance while interface can be used as multiple inheritance.
  • An abstract class use constructor while in an interface we don’t have any type of constructor.

More Questions Like This

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