Click here to Skip to main content
13,147,956 members (74,948 online)
Rate this:
Please Sign up or sign in to vote.
See more:

when will use abstract class and when will use interface can you give sample
Posted 3-Nov-12 6:22am
Sandeep Mewara 3-Nov-12 15:39pm
Just search here at CP using keywords and you will find it discussed multiple times.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2017
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