Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

If an abstract class and an interface have same members without body methods then what is the difference between them.

Thanks
Posted
Updated 1-Apr-11 1:10am
v4
Comments
apaka 1-Apr-11 3:46am    
Try this article http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
Wild-Programmer 1-Apr-11 3:47am    
Abstract class still has an option of defining the methods at later stage, whereas interfaces dont.

There is an article which covers this in some depth here: Abstract Class versus Interface[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-11 5:27am    
Sure, my 5.
And I referenced our past discussion, it was useful.
--SA
Albin Abel 1-Apr-11 6:44am    
Good link. my 5
[no name] 1-Apr-11 22:54pm    
This is only a link.
Why you people make things complex even when it can be understood by some lines. OP does not know the difference and you are giving the link. "Thats wonder".... ha
Albin Abel 2-Apr-11 0:59am    
See my comment on your answer. You need to learn. Thats why the link. You can see your few lines does not work well every time, but misleading.
OriginalGriff 2-Apr-11 3:32am    
I give the link because it is a complex subject, and needs some careful handling. You can do a lot more in an article than you can in a Q&A answer - images for example - which can make the discussion of complicated subjects easier to understand.
If I just wrote a simple explanation here myself, it would be just that: simple. The topic deserves more than that.
In addition to the reference by Griff, please read this discussion: When we use abstract and when we use interface...?.

[EDIT]
One more important thing: in .NET structures cannot inherit from non-interfaces. They can implement muliple interfaces though. This is practically the only OOP feature applied to structures (and value types in general). Most developers did not pay attention to this important feature (unfortunatly, including myself, until someone brought my attention to it).

—SA
 
Share this answer
 
v3
Comments
Albin Abel 1-Apr-11 6:44am    
Good discussion. My 5. I have added few more discussion here. So the discussions accumulate and the link refresh for future answers. :). My 5
Sergey Alexandrovich Kryukov 1-Apr-11 11:12am    
Thank you, Albin.
--SA
CPallini 1-Apr-11 7:06am    
As you well know the 'interfaces' are the toy language expedient for mocking multiple inheritance...
***** **
** *** *
** *** *
***** **
Sergey Alexandrovich Kryukov 1-Apr-11 11:16am    
I think the talking about "toy" and "real" languages have not rational background and only shows your imprinting by "the one which should not be named" :-).

What is really good to remember is that COM interface was modelled over a C++ classs without data with all pure virtual method (call it "purely abstract class"); and this form is currently used to support interfaces in C++.

--SA
CPallini 1-Apr-11 13:10pm    
Well, of course I don't think C# and Java are toy languages. That said, C++ has interfaces (yes, pure abstract classes are interfaces). COM, on the other hand was a (brilliant if you ask me) attempt to share native objects among languages.
I hope the links given by SAKryukov and Original Griff give you a clear picture. I am agree with the comment of Amit Kumar Tiwari. But the point I am stress here is there is no hard rule which one to use. It depends on your necessity. Let me give you a situation which favors Interface over Abstract.

Let us say you have a class Car. It has an abstract method? CalcFuelEfficinecy. Then you will end up defining this method in all your child classes, even the method is same for a few of those children. You can visualize repeatability of same code. As a rescue you want to move this method to your abstract class?. Can't right as a few children want this method in a different way. May be you end up messing with overloading.

But one thing clear this has to taken out of the class and deal separately. So how about defining a IEfficiencyCalculator and it has a signature for the CalcFuelEfficiency method?. Then interface will be implemented by a set of classes say EfficiencyCalculator1, EfficiencyCalculator2.... and so on. Now you can have a instance member of this interface type in your parent class and assign the required EfficiencyCalculator at runtime.. You can see now the problem solved and you can have any number of efficiency calculation business logics and same business logic not repeated. This pattern otherwise called a strategy pattern. You don't need to have a abstract class again to defined these strategy composites. Because here we only define a contract. A class needed only when we use the oops concepts like encapsulation, inheritance etc.,

Now coming back to Amit Kumar Tiwari's comment. An abstract class allows defining a method later stage. Agreed. This is an advantage or disadvantage?, I think it depends your requirement again. If you are going to implement/define this new method in the abstract class then I see there is a potential problem. From the example the Car inherited by Car1. Later you add a method to Car, of course it is useful method for many children, but not required for car1. So you must override this in car1 and do nothing, otherwise the classes inherit car1 will access this method and misuse it. But this problem not there in interfaces. It ensures a safe contract.

Again coming back to Amit Kumar Tiwari's comment that abstract class favors adding new method. Good point. But how to use it for flexibility. Define the interface as said earlier. Use an abstract class and abstract methods to implement this interface. Then only necessary methods can be implemented in the children. So when you add new method in interface , then the least change we need to do only at the abstract class and the necessary children. Other children not affected. So interface still offers the flexibility to add new methods at later stage in a safer way than the inheritance path.

Cheers!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Apr-11 11:17am    
Excellent. My 5.
--SA
Albin Abel 1-Apr-11 12:50pm    
Thanks SAKryukov
Differences are as Follows:
1. Interface is Globally accessible but Abstract class is not.
2. Abstract class can have a method with Body but not an interface.
3. Abstract class can have a abstract or non-abstract method but methods in Interface are abstract by-default.
4. All Methods in Interface are public by-default but not in abstract.
5. Abstract class can have methods or attribute but interface cannot have an attribute.
 
Share this answer
 
v2
Comments
Albin Abel 1-Apr-11 12:49pm    
What you mean by 1. Interface is Globally accessible but Abstract class is not.?
Sergey Alexandrovich Kryukov 1-Apr-11 13:01pm    
Igree. It probably does not make sense.

I recently added important item I did not pay attention for: structures can implement interfaces!
My Answer updated.
--SA
Albin Abel 2-Apr-11 0:48am    
Thanks for the information about structures. Brijesh's first point is still questionable. His explanation for global accessible is access across applications. But interfaces by default marked internal. So I am not sure about the validity of his first point.
Sergey Alexandrovich Kryukov 2-Apr-11 16:24pm    
I would say it is invalid as formulated. May be the indent was different.
--SA
Albin Abel 3-Apr-11 0:20am    
May be, Yes

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