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

when will use abstract class and when will use interface can you give sample
Sandeep Mewara 3-Nov-12 15:39pm
Just search here at CP using keywords and you will find it discussed multiple times.

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.
Abhishek Pant 3-Nov-12 13:51pm
Sushil Mate 3-Nov-12 23:44pm
Thanks abhishek.

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

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