Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the different between the Interface and abstract base class?
The methods in the abstract base class cannot be achieved, It is as same as the methods in interface.
If I can use the abstract base class to achieve the function, why I do define a Interface.
what is the competitive advantage of the interface?
Posted

Two things:
1) An interface cannot contain any implementation at all: all it's methods are virtual. An Abstract class can contain real methods, which may or may not be overridden in the derived class.
2) You can only derive from one base class, but you can implement as many Interfaces as you need. You do not derive from an interface, only from a class.
 
Share this answer
 
Comments
Zhang, G. 11-Apr-14 21:48pm    
Thank you!!!!
Roughly speaking, an interface is just a contract the implementing class has to observe. On the other hand the abstract class does provide some functionality, however you have to complete it (in the derived class) in order to use it.
 
Share this answer
 
Comments
Zhang, G. 11-Apr-14 21:48pm    
Thank you!!
Here is detailed description



Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that
have to be overridden.
Access Modfiers
An interface cannot have access modifiers for the subs, functions, properties etc
everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words
both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for
objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use
Interfaces.
If various implementations are of the same kind and use common behaviour or status
then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations
of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing
default implementation and therefore all the existing code might work properly.
Fields and Constants
No fields can be defined in interfaces
An abstract class can have fields and constrants defined
 
Share this answer
 
v3
Comments
Zhang, G. 11-Apr-14 21:48pm    
Thank you!!
Er. Puneet Goel 12-Apr-14 7:40am    
hey do accept the answer if you likes it

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