This is a favorite question of any interviewer. Probabilities are less where interviewer expects syntactic differences rather one might be looking at design level differences. Next question which follows is "When do you prefer interface over abstract classes" or other way round. I have collected answers over the internet to compile one simple page to answer this question.
Below is from John Noah's interview questions comiplation:
Below answers are from MSDN.
Below is from John Noah's interview questions comiplation:
An abstract class can provide complete, default
code and/or just the details that have to be
overridden.
|
An interface cannot provide any code at all,just
the signature.
|
In case of abstract class, a class may extend
only one abstract class.
|
A Class may implement several interfaces.
|
An abstract class can have non-abstract
methods.
|
All methods of an Interface are abstract.
|
An abstract class can have instance variables.
|
An Interface cannot have instance variables.
|
An abstract class can have any visibility:
public, private, protected.
|
An Interface visibility must be public (or)
none.
|
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.
|
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.
|
An abstract class can contain constructors .
|
An Interface cannot contain constructors .
|
Abstract classes are fast.
|
Interfaces are slow as it requires extra
indirection to find corresponding method in the
actual class.
|
Below answers are from MSDN.
When shall we prefer Abstract classes or Interface over other?
- If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
- If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
- If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
- If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Good explanation. Interface have no super most interfaces.Abstract class have super most (root) class called Object.
ReplyDeleteprathap
dot net training in chennai
Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
ReplyDeletepython training Course in chennai
python training in Bangalore
Python training institute in bangalore
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Certification Training Online Toronto
AWS Certification Training Online Newyork
AWS Certification Training Online London
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteAWS training in chennai
AWS Training in Bangalore
After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.thanks a lot
ReplyDeleteC and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training