Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the difference between Interface and Abstract Class?

user-image
Question ajoutée par Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb
Date de publication: 2016/10/20
Amit Sharma
par Amit Sharma , Systems Analyst , TCS

Abstract  Class:

1. An Abstract class is a class which contains zero or more abstract members(methods, properties, event, and indexer).

2. It is mandatory to give implementation to all the abstract members in the derived class when you are inheriting your class from abstract class otherwise you need to declare your class as abstract class.

3. You need to create an abstract class when you have a family of related classes which share some common behavior and some uncommon behavior.

 

Interface:

1. An interface is a group of members with empty bodies.

2. Any non-abstract type inheriting the interface must implement all its members.

3. Within interface no access modifier is allowed and by default access modifier is pulic.

4. When we have series of unreleted classes which is not having any common behavior, it is better to implement interface.

Pathan Zarrar Khadar Khan
par Pathan Zarrar Khadar Khan , Windows System Administrator , Dyce Tech Pvt Ltd

  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.
  6. Abstract Class : We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.

    7.In C# , an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.

Atif Jalal
par Atif Jalal , Front-End Web Developer , Brain Plow

An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.

anas alhashki
par anas alhashki , Senior Full Stack Engineer , Industrial Motor Power Corporation

  •  Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
  •  Abstract class doesn't support multiple inheritance.Interface supports multiple inheritance.
  •  Abstract class can have final, non-final, static and non-static variables.Interface has only static and final variables. Abstract class can have static methods, main method and constructor
  • .Interface can't have static methods, main method or constructor.
  • Abstract class can provide the implementation of interface.Interface can't provide the implementation of abstract class.
  • The abstract keyword is used to declare abstract class.The interface keyword is used to declare interface.

Example for abstract class:

public abstract class Shape

{

public abstract void draw();

}

Example for interface class:

public interface Drawable

{

void draw();

}

Igor Polischuk
par Igor Polischuk , Software Engineer , EPAM

I will try to describe my answer very briefly.

Interface can't include any implementation. An interface is a set of method signatures (of course, interface can also define parameters and events). Also, you can inherit your class from several interfaces at the same time.

Abstract class can include behaviors which have an interface. But you can inherit your class from only one abstract class (a class is always derived from only one class). You are able to mark abstract methods as public, internal and protected. But interface's methods can be marked only public.

In my opinion, it's main differences.

Waqas Ahmed
par Waqas Ahmed , Tech. PM / Software Designer , Aga Khan University Hospital

The most important difference between Interfaces and Abstract Classes is the semantic difference.

An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is.

Technical differences between Abstract Classes and Interfaces is that an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes. A class can implement multiple interfaces, but it can only inherit one class (abstract or otherwise).

 

Awwab Ahmed
par Awwab Ahmed , Web-Developer, Graphic-Designer. , Conobio

An interface is a contract: the guy writing the interface says, "hey, I accept things looking that way", and the guy using the interface says "Ok, the class I write looks that way".

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It's more about a guy saying, "these classes should look like that, and they have that in common

More Questions Like This