Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: (untagged)
while designing the project, may i know which provides best performence either using interfaces or abstract classes
Posted
Comments
Sergey Alexandrovich Kryukov 7-Jun-12 23:59pm    
In general, this cannot be a correct question: it depends on what operations do you want to compare in performance.
--SA

Sorry if you consider this as not an answer: first, it depends on how you use them. Please see my comment to the question. Secondly, this question can present only the sport interest, something like cockroach or frog races. In practice, there won't be any difference at all, or, if there is some, it should be so insignificant so it should not be taken into account in any decision. At the same time, the difference in flexibility, code quality, architectural and expressive capabilities between abstract class based and interface based polymorphism is so formidable that is makes performance considerations related to this choice just ridiculous. At the same time, static call dispatching without virtual methods and interfaces is always a bit better, by apparent reasons; and this is often taken into consideration.

Performance is only important when the throughput and responsiveness of the whole application are concerned, but it is achieved using very different approaches. Unfortunately, this is not the time to discuss it, because it would need a whole big book to cover at least essential part of this topic.

So, by the reasons I explained above, this question is not interesting enough to make me doing some measurements; please don't blame me for that. However, you can easily perform the measurements to compare the operations you are interested in. To get sufficient accuracy, you will need to use the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

To make sure you get fine time resolution and to learn the resolution, you can use its static properties IsHighResolution and Frequency.

Have fun, but my prediction is: you won't find anything interesting or practically significant.

About other aspects of interfaces vs. abstract base classes, please my past answers and other answers in these discussions:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

Nobody discussed performance though, perhaps because all participants understood it wasn't a point.

—SA
 
Share this answer
 
Interface and abstract class don't have any significant difference when it comes to performance. Also, using interface or class merely considering the performance factor would be wrong. this is more of a design decision that which should be used. so don't bother about performance just think about design and whichever suits your design use it, performance will be almost same. (I have not statistical proof to back me here but if someone spend the time in doing this exercise he will agree with me. also most people will agree that this is a design decision rather than performance measure.)

as for the which to use when, questions have been asked for that already
This is one of the answers I posted in response to a similar question
------------------------
Interface and abstract class both are used to serve two purpose

1. Having the facility for objects to work in polymorphic fashion.
2. Some kind of contract is enforced on the objects( the interface primarily defines the contract)

Now the decision of using which is based on

If you have only contract that should be enforced you should use interfaces.

But if you have some default behavior along with the contract that the derived classes should/could use then you should use abstract classes.

But I will not suggest going for either/or, My suggestion would be

1. Have an interface defining only the contract.
2. define an abstract class that implements this interface and then defines the default behavior functions.
3. Then have the concrete classes use the abstract classes. (they may choose to override some methods).

more like

Interface <------AbstactClass <---------------ConcreteClass

Gurus, please correct me if i said something wrong here.
 
2. and here is another one of similar nature
-------------------------------------------------------------------
Question asked already and here are the accepted answers:

My answer:
Well you are looking it the wrong way. It is not that you need to create interface always if you have to write classes. The interface are required when
 
1. You need multiple classes to behave in a polymorphic way.
2. You need some kind of contract to enforce on the classes. put the contract in interfaces.

and then a very awesome answer by SAKryukov

Let's start with the short answer by Rahul Rajat Singh. It's basically correct, but does not show the cases where only the interface could be used. Let me use this answer as the base and go from there.
 
In both cases, one could use not just the interface, but also a common base class; and that class could be purely abstract (by "purely" I mean that it might not have non-abstract members at all, in this case it is seemingly equivalent to interface). If so, why interfaces, why not always base classes?
 
Well, two things.
 
An interface could be implemented not only my class, but also by a structure, which is a value type, not a reference type. Polymorphism based on interfaces is more flexible then the one based on the base class. Polymorphous containers become agnostic not only to the run-time type of the elements but even to the nature of implementing type: it can be either value or reference type. It gives more flexibility.
 
More importantly, interfaces and interface implementing types are way more flexible in inheritance: even though a class can have only one immediate base class, a class or a structure can implement more then one interface; and each interface can have more than one interface in inheritance list. This is called weak form of multiple inheritance. In practice, this makes it possible to develop some variants of usage impossible with classes. For example: 1) a class based on some class irrelevant to some polymorphous set can still become a member of the set as we can derive a class implementing required interface; 2) we can make objects participating in two or more different polymorphous sets; to do so, it's enough to implement two or more different interfaces by the same class or structure.
 
-SA
 
Share this answer
 

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