g.soma shekar
Answered On : Dec 15th, 2005
abstract classes are designed with implemantion gaps for sub-class to fill in.
interfaces are sintacticlly similar to classes but they lack insance variables & methods.
abstract classes can also have both abstract methods & non-abstract methods. where as in interface methods are abstract only, & variables are implicitly static&final.
Login to rate this answer.
umesh choubey
Answered On : Jan 9th, 2007
hai ,
when u want to get the sub class(type) property into the super class(type),that we have to achive by using abstact class ,interface is one design we can se.
other wise both think work same
Thank & Regards
umesh choubey

1 User has rated as useful.
Login to rate this answer.
When we do not want any one to create object of our class we define the class as abstract.
when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will.
thank you
Login to rate this answer.
Sunny
Answered On : Jul 8th, 2007
Every body is just explaning what is that........ butwhat about how is that?
Login to rate this answer.
Bharath
Answered On : Oct 15th, 2007
Abstract classes can perform the same operations as interfaces. But if we want to make a compulsory implementation of the functions, we have to use abstract class.
Also, use interfaces when you want to allow the user to inherit another class and use this as a sub-type.
Login to rate this answer.
We will use abstract class when we want that the parent class must implement some default behavoiur and keep some behavoiur to be imlemented by parent Class
Login to rate this answer.
Interface provides method signature and not the body. Its up to the developers to
provide body for these methods once they implement it.
Abstract provides both, methods with and without body. Adding new methods in abstract class in middle of development life cycle won't break existing sub classes unless these methods are declared as mustoverride. If there are frequent addition of new methods, properties and so on., one should use abstract..
Adding new methods to the interface will force users to go through each and
every sub class that implemented that interface and call these methods.
So its always preferred to use abstract over interface because of this limitation.
Whereas on the other hand interface can fill gap of multiple inheritance. One can
implement from more than one interface. Not same with Abstract. One can inherit
from only one abstract class.
If you want to inherit from class that can provide reusable code then use abstract class. For instance, connecting to the database or base class for grid or treeview.
If you implement your subclass from an interface then in each subclass you will have to write your own code to fill body of these methods.
Hope this helps.
Thanx
Tins30
Login to rate this answer.
"adarsh83 wrote:
|
When we do not want any one to create object of our class we define the class as abstract. when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will." |
That will still leave me wondering... When would someone not want to create object of some class? You also mentioned about when to define an interface.. But same is true for abstract class.. We can use abstract class to do exactly same thing that you
stated for interface.. Plz correct me if I am wrong.
Thanx
Tins30
Login to rate this answer.
Interface:
1. If u want to implement all of the methods than we go for interface.
2. In future to put any methods that dows not effect.
3. To implemnting Inheritence Future
Abstarct:
1. when it necessay dont create the instance of the class for the security purpose.
Login to rate this answer.
Karuna Reddy wrote:
"
Interface:
1. If u want to implement all of the methods than we go for interface.
2. In future to put any methods that dows not effect.
3. To implemnting Inheritence Future
Abstarct:
1. when it necessay dont create the instance of the class for the security purpose. "
Underneath Interface:
Point 1. Right
Point 2. I believe this is not truly accurate.. Not trying to be instigator!!
If you want to put any methods that does not affect(I believe you meant that does not affect other class) then I would use abstract class not interface. Here's an explanation why? For instance, if you are already implementing from an interface in 10 different sub classes and if you add another method to the interface then you will have to go through each and every sub class and declare this method. Else you will get an error. So I believe point 2 should be under Abstract. Thats why I always prefer abstract over interface. Interface is bit more strict on implementation than abstract on inheritance.
Point 3. Interface supports multiple inheritance but C# does not truly support multiple inheritance like C++ and so one can use interface and can also implement from more than 1 interface.
Login to rate this answer.
The operation of abstract class is same as interface but a minute difference between them. when we want to apply all the functionality of class or interface in the extended or implemented class or interface, then in that condition, we use interface for example if we have two method like getName() and getAddr() in the interface and we define(override) only one method like getName() or getAddr() then JVM will report compile time error untill we define those two methods(getName() & getAddr()).
But if we are using abstract class then iabstract class provides flaxbility to the user because if those two methods say 'getName() and getAddr()' defined in abstract class,then we extends that abstract class and as per our need override any one or both the methods.
except that if we want to operate on java classes using object then we use abstract class else interface.
Nidhees Kumar Singh(Kalwari,Balrampur)
Login to rate this answer.
When i have to extends a class, then in that case it is better to use Interface. Suppose that i have a class Product, and i have to extends class Customer into this class. So if i have extended a Abstract class earlier, in that case i would have not be able to again extends this Class Customer, because Java does not give facility of the Multiple inheritance. So to implement the multiple inheritance in out code we use Interfaces over the Abstract class.
Login to rate this answer.
aparna
Answered On : Dec 19th, 2011
If you want common functionality(generalization) for some classes go for abstract
If you dont want generalization then go for abstract class
Login to rate this answer.
seenthil
Answered On : Mar 25th, 2013
1. If you are creating something that provides common functionality to unrelated classes, use an interface.
2. If you are creating something for objects that are closely related in a hierarchy, use an abstract class.
Login to rate this answer.