Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi,
 
when will use abstract class and when will use interface can you give sample
Posted 3-Nov-12 6:22am
Comments
Sandeep Mewara at 3-Nov-12 15:39pm
   
Just search here at CP using keywords and you will find it discussed multiple times.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Quote:
Abstract & Interface
• An abstract class can have shared state or functionality. An interface is only a promise to provide the state or functionality. A good abstract class will reduce the amount of code that has to be rewritten because its functionality or state can be shared. The interface has no defined information to be shared.
• Root point is that abstract classes ensure that all descendants share functionality/state, where an interface only ensures a common bond.
• Abstract classes allow you to create a blueprint, and allow you to additionally CONSTRUCT (implement) properties and methods you want ALL its descendants to possess.
• An interface on the other hand only allows you to declare that you want properties and/or methods with a given name to exist in all classes that implement it - but doesn't specify how you should implement it. Also, a class can implement MANY interfaces, but can only extend ONE Abstract class. An Interface is more of a high level architectural tool
• Why use one over the other? The former allows for a more concrete definition of descendants - the latter allows for greater polymorphism.
• Abstract classes can be Derived whereas Interfaces can be Implemented. There is some difference between the two. When you derive an Abstract class, the relationship between the derived class and the base class is 'is a' relationship. e.g., a Dog is an Animal; a Sheep is an Animal which means that a Derived class is inheriting some properties from the base class. Whereas for implementation of interfaces, the relationship is "can be". e.g., a Dog can be a spy dog. A dog can be a circus dog. A dog can be a race dog. This means that you implement certain methods to acquire something.
  Permalink  
Comments
Abhishek Pant at 3-Nov-12 13:51pm
   
Excellent!
Sushil Mate at 3-Nov-12 23:44pm
   
Thanks abhishek.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
This article can help you:
Abstract Class versus Interface[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 410
1 Abhinav S 260
2 Nirav Prabtani 252
3 Andreas Gieriet 155
4 Dave Kreskowiak 155
0 OriginalGriff 7,545
1 Sergey Alexandrovich Kryukov 6,757
2 Maciej Los 3,909
3 Peter Leow 3,693
4 CHill60 2,712


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100