When to use Interface over abstract class?

Abstract Classes: Classes which cannot be instantiated. This means one
cannot make a object of this class or in other way cannot create
object by saying  ClassAbs abs = new ClassAbs(); where ClassAbs is
abstract class.

Abstarct classes contains have one or more abstarct methods, ie method
body only no implementation.

Interfaces: These are same as abstract classes only difference is we
an only define method defination and no implementation.

When to use wot depends on various reasons. One being design choice.

One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.

Showing Answers 1 - 38 of 38 Answers

g.soma shekar

  • 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.

  Was this answer useful?  Yes

umesh choubey

  • 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

adarsh83

  • May 10th, 2007
 

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

  Was this answer useful?  Yes

Sunny

  • Jul 8th, 2007
 

Every body is just explaning what is that........ but what about how is that?

  Was this answer useful?  Yes

Bharath

  • 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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Tins30

  • May 29th, 2008
 

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

  Was this answer useful?  Yes

Tins30

  • May 30th, 2008
 

"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

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Tins30

  • Jun 3rd, 2008
 

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.

  Was this answer useful?  Yes

Nidhees

  • Jul 23rd, 2010
 

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)

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

aparna

  • 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

  Was this answer useful?  Yes

seenthil

  • 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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions