Click here to Skip to main content
14,868,639 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Some confusion about abstract class and Interface.
What is difference between a abstract class with all method abstract and Interface?
e.g
C#
Interface IData
{
void Save();
}
abstract class AData
{
public abstratct void Save();
}


What is difference between Both?

How Interface is more secure than abstract class?
Why dependency Injection always preferred to use Interface not abstract class ?
while in constructor DI injector we can pass abstract class which is same functionality as Interface.
e.g
C#
--===For Interface
Interface IData
{
void Save();
}
public class Sql:IData
{
}
public class Oracle:IData
{
}
public class Client
{
IData _data=null;
Public Client(IData data)
{
_data=data;
}
}
--===For abstarct class
abstract class AData
{
public abstratct void Save();
}
public class ABSql:AData
{
}
public class ABOracle:AData
{
}
public class Client
{
AData _data=null;
Public Client(AData data)
{
_data=data;
}
}

What is difference between both?
Posted
Updated 22-Feb-16 22:15pm
v2
Comments
Richard MacCutchan 23-Feb-16 3:26am
   
This is a very common question and Google will find you many papers that explain the differences.

First of all, nearly all "difference" questions are invalid. If you don't understand why, try to tell us the difference between apple and Apple. :-)

Simply learn both concepts, study all the detail, use cases, and so on.

There is really a lot to it. On the design choice between them and some motivation, please read my past answers on the topic:
Why we need to create instance for interface not for implementation?[^],
Doubts on Interfaces[^],
If abstact class implements the interface then is there any need to implement interface method in abstract class.[^],
POLYMORPHISM WITHOUT OVERLOADING AND OVERRRIDING IS POSSIBLE[^],
Interfaces and Polymorphism[^],
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^].

—SA
   
Comments
Er. Dinesh Sharma 23-Feb-16 6:05am
   
Alexandrovich thanks for the link you have provided above. But still I am not getting answer of one question.
Please help to answer this question.
Why dependency Injection always preferred to use Interface not abstract class ?
   
Why would you say "always"? Anyway, I think this consideration of design choices on dependency injection is good enough: see, specifically, Interface_injection_comparison: "The advantage of interface injection is that dependencies can be completely ignorant of their clients yet can still receive a reference to a new client and, using it, send a reference-to-self back to the client. In this way, the dependencies become injectors. The key is that the injecting method (which could just be a classic setter method) is provided through an interface." Then methods of injections are illustrated on samples...
—SA
Abstract Class:
Abstract class provides a set of rules to implement next class
Rules will be provided through abstract methods
Abstract method does not contain any definition
While inheriting abstract class all abstract methods must be override
If a class contains at least one abstract method then it must be declared as an “Abstract Class”
Abstract classes cannot be instantiated (i.e. we cannot create objects), but a reference can be created
Reference depends on child class object’s memory
Abstract classes are also called as “Partial abstract classes”
Partial abstract class may contain functions with body and functions without body
If a class contains all functions without body then it is called as “Fully Abstract Class” (Interface)

Interface Class:

If a class contains all abstract methods then that class is known as “Interface”
Interfaces support like multiple inheritance
In interface all methods r public abstract by default
Interfaces r implementable
Interfaces can be instantiated, but a reference cannot be created
   
Comments
   
After the phrase "If a class contains all abstract methods then that class is known as “Interface”, no need to read the rest. "A reference cannot be created" — total gibberish. All wrong. In ".NET" (and a number of other technologies), interface types are not classes, despite of common origin of the concept and possibly identical binary layout (not with .NET though).

I would recommend to answer question if you are an expert in the subject. Remember that confusing some naive reader may cost them dearly.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900